[zeromq-dev] random crashes on zmq_ctx_term() when there is a monitored socket

Peter Kleiweg pkleiweg at xs4all.nl
Fri Oct 31 17:24:44 CET 2014


  1. create a context
  2. create socket
  3. put monitor on socket with zmq_socket_monitor()
  4. socket: connect to address
  5. socket: remove connection
  6. terminate the context

Repeat this often enough, and either the program will hang at 
step 6, or crash with an error at step 6. The errors vary. These 
are the ones I have seen:

     Bad file descriptor (signaler.cpp:269)
     Assertion failed: pfd.revents & POLLIN (signaler.cpp:226)
     Invalid argument (mutex.hpp:99)
     Resource temporarily unavailable (signaler.cpp:269)
     Segmentation fault

The problem disappears if you have a thread (or goroutine) 
actually reading the messages from the monitor, and if you 
insert a sleep (0.1 seconds) between steps 5 and 6 as well.

Problem observed with ZeroMQ 3.2, 4.0 and 4.1, on linux/amd64, 
using the Go interfaces zmq3 and zmq4. 

Please see details and code examples here:


My guess of what is going on is this: The monitor is running in
its own thread, while zmq_ctx_term() tries to close the
monitoring socket in the main thread. Thus, the socket would be
used in two threads at once, and since sockets aren't
thread-safe, a crash follows.

Peter Kleiweg

More information about the zeromq-dev mailing list