[zeromq-dev] Poll about linger and termination behaviour!

Martin Sustrik sustrik at 250bpm.com
Sat Mar 12 15:40:21 CET 2011


Hi all,

Unfortunately, there was backward incompatible change introduced with 
0MQ/2.1.0

Namely, there was "linger" functionality introduced which allows a 0MQ 
socket to linger on after it have been zmq_close'd so that it's able to 
send any pending messages instead of dropping them.

The problem is that with 2.1.0 the default linger value was set to -1 
(infinite) instead of fully-backward compatible value of 0 (drop any 
pending messages immediately).

Thus we have a problem when moving to stable version of 2.1. Either we 
preserve the linger value of -1 as found in 2.1.0 and 2.1.1 and thus 
break 2.0 application migrating to 2.1 or we revert to 2.0's behaviour 
by setting the default to 0 and thus break applications migrating from 
2.1.0 and 2.1.1 to 2.1.2.

In technical terms, default value of -1 (infinite) has a problem that 
non-accessible peer can cause zmq_term() to hang forever, trying to send 
pending messages:

int main ()
{
    void *ctx = zmq_init (1);
    void *s = zmq_socket (ctx, ZMQ_REQ);
    zmq_connect (s, "tcp://localhost:5555");

    zmq_msg_t msg;
    zmq_msg_init (&msg);
    zmq_send (s, &msg);

    zmq_close (s);

    zmq_term (ctx); // blocks here forever
}

Other way round, if default is set to 0, the problem is that if a 
message is sent and the application is terminated immediately 
afterwards, the message probably won't make it to the peer and will get 
dropped:

int main ()
{
    void *ctx = zmq_init (1);
    void *s = zmq_socket (ctx, ZMQ_REQ);
    zmq_connect (s, "tcp://localhost:5555");

    zmq_msg_t msg;
    zmq_msg_init (&msg);
    zmq_send (s, &msg); //  msg will never get delivered to the peer

    zmq_close (s);

    zmq_term (ctx); // msg gets dropped here
}

There is no obvious solution for the problem. Rather, the intent is to 
cause as little pain as possible. Thus, please, let us know which 
behaviour is preferable for you. Vote for either "infinite" default or 
zero default. The solution that gets more votes will make it into next 
version of 2.1

Thanks!
Martin



More information about the zeromq-dev mailing list