[zeromq-dev] flushing (again)

Martin Sustrik sustrik at 250bpm.com
Sun Apr 3 17:32:35 CEST 2011


Hi Andrew,

> i am running 2.1.1.
>
> i have a program which essentially does
>
> // sock has been opened as a ZMQ_PUSH over tcp
> msendstr(sock, "exit");
> ret = zmq_close(sock);
> assert(ret == 0);
> exit(0);
>
> if i run this, then i've never seen the message get to teh other end.
> if i insert a sleep(1) BEFORE the exit (and AFTER the zmq_close), it
> always arrives at its dest.

The problem is that you don't terminate the library (zmq_term).

zmq_close is an async operation that starts shutdown of the socket and 
returns immediately. The shutdown -- including the sending of pending 
messages -- continues in the backgroud. If you exit the process at this 
point, the background thread won't get a chance to push the messages to 
the network.

So, instead of exiting, just call zmq_term(), which is an blocking 
operation and waits for all pending messages to be sent.

Unfortunately, 0MQ is implemented in user space and thus doesn't allow 
for nice, transparent, TCP-like flush of pending messages.

Martin



More information about the zeromq-dev mailing list