Zmq_ctx_term () blocks with closed sockets

I am working with ZMQ and am facing problems while destroying the context.

I have code

zmq_ctx_shutdown(context);
zmq_ctx_term(context);

      

This code always locks on zmq_ctx_term()

, and also locks with zmq_ctx_destoy()

; however, if this call is removed and only the shutdown call is used, everything works fine, but the application will leak memory from not freeing the zmq context.

In my code, I have four sockets: two sockets ZMQ_PAIR

inproc

that are used for communication between the main thread and a thread using a socket ZMQ_REP

, and ZMQ_PUB

one that runs on the main thread.

I have set ZMQ_LINGER

on all of these sockets to 0, as it seems like the only time the call zmq_ctx_term()

should block if messages have not been sent yet, as I called zmq_close()

on all sockets, which returns without error. Also, this lock persists if I only call zmq_ctx_new()

, create sockets and then call zmq_ctx_shutdown(); zmq_ctx_term();

.

I'm afraid there is something wrong with using two sockets inproc

for communication between threads with the same zmq context, although there seems to be no problem with the message when receiving messages.

After suffering that I might have a thread safety issue, I found that the ZMQ context needs to be thread safe, but not sockets. I have checked and on the main thread, I open the socket close ZMQ_PUB

and inproc

. On another thread, I open and close the socket ZMQ_REP

and the other end of the socket inproc

, so it seems like it shouldn't be a problem.

For clarification, I'm writing my code in C using libzmq from the current master branch on GitHub (commit d35473e). I've seen similar problems when linking the ZMQ shared library, although this happens regardless of whether I'm using a static or shared library. I am currently on OS X 10.9.

If anyone has time to look at the code in general, the corresponding file will be listed here .

Any idea what is going on here?

+3


source to share


1 answer


Notes:

I had the same problem. Even though I was closing the type socket ZMQ_REQ

and zmq_close()

returning , it still blocked. This only happened after sending a message to a peer that didn't work. 0

zmq_ctx_term()

zmq_send()

will return immediately but zmq_recvmsg()

disconnect.



After that I would close the socket and call zmq_ctx_term()

. Apparently, even though it zmq_close()

returned , the sent message was still in the outgoing queue and will be blocked for that reason . 0

zmq_ctx_term()

Decision:

I solved the problem by setting the parameter to half the value . This behavior is indeed described here http://api.zeromq.org/4-0:zmq-ctx-term ZMQ_LINGER

ZMQ_RCVTIMEO

+3


source