[zeromq-dev] Closed dealer socket may cause context termination to hang

André Caron andre.l.caron at gmail.com
Thu Nov 6 01:13:28 CET 2014

Hi there,

I've been bit by this nasty little bugger and I thought I'd share.

In this little snippet, terminating the context hangs unless the connection
was established, even though we're closing the socket properly before
terminating the context.

    import zmq
    c = zmq.Context()
    s = c.socket(zmq.DEALER)
    s.send_multipart(['foo', 'bar', 'meh'])
    c.term()  # hangs (until someone starts listening on port 666).

It happens that if you put a linger on the socket when closing it, the
program shuts down nicely.

Took me a while to figure out why, but it seems this is a symptom of that
little subtlety between bind and connect in ZeroMQ.  I remember reading
about this somewhere in the guide, but I probably thought I'd come back to
this concept and never did.  So apparently, the dealer socket immediately
creates a queue for messages when you call "zmq_connect()", even if the TCP
connection is never established and somehow this queue that never gets
flushed if you don't establish a connection.

I'm really surprised by this behavior.  I would think that closing the
dealer socket would stop the reconnection attempts, and then the socket
would drop the queue and allow the context to terminate (as when I set the
linger on the socket).

I know the queue creation on "zmq_connect()" is by design, but is the
attempt to connect to the peer after the dealer socket is closed desired as

If it is, I'd like to know if there is a place to document these kinds of
gotchas.  If so, I'd probably send in a documentation patch for this
particular one.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20141105/0eb9b070/attachment.htm>

More information about the zeromq-dev mailing list