[zeromq-dev] term() would not exists if any sockets are not closed

Pieter Hintjens ph at imatix.com
Fri May 27 18:41:22 CEST 2011

On Fri, May 27, 2011 at 7:20 AM, Pavel Chlupáček <chlupacek at gmail.com> wrote:

>  I just wonder if there is no any way how to force all sockets to close when term() is called. So far it blocks indefinitely the calling thread, and I wonder if there isn't any way to avoid that.
>  I experimented a bit with linger opt, but did not get any visible result from it.
>  I am on scala (java bindings) so hope it is not specific to java.

There is a long discussion about this. The upshot is that because 0MQ
allows sockets to be migrated between threads, it cannot automatically
close sockets at zmq_term time.

In the CZMQ binding, I wrapped this up so that it does in fact close
sockets automatically. This assumes there is no socket migration. I
know the erlzmq binding does more or less the same, and it could be
done in any language binding.

The approach is to track all open sockets, and when the called wants
to destroy the context, to (a) set LINGER on all sockets to some
specific value, be it zero, 1 second, etc. and (b) close all sockets,
and (c) call zmq_term.

In any other thread that's using the same context, blocking calls will
exit with ETERM, and those threads must then also do (a) and (b) in

This cannot be done by libzmq without breaking socket migration. It
can be done in a language binding. If you were for instance to wrap
CZMQ in Scala, you'd get that behaviour in Scala.


More information about the zeromq-dev mailing list