[zeromq-dev] zmq_term hanging

Pieter Hintjens ph at imatix.com
Tue Sep 24 10:52:27 CEST 2013


The problem isn't in your design, it's in libzmq's shutdown handling,
and default choice of infinite linger, which makes simple programs
hang forever without reason.

Setting linger to zero before calling zmq_close() should be enough.

-Pieter

On Tue, Sep 24, 2013 at 10:40 AM, Morten Møller Riis
<mortenmoellerriis at gmail.com> wrote:
> Thank you Pieter!
>
> Could I get you to expand a bit on why my design is poor? Is it just because
> that it will linger forever, or am I doing something completely wrong here?
>
> I'll look into czmq.
>
> I still have the problem if I set the linger to 0 before binding the socket
> though.
>
>
>   int linger = 0;
>   zmq_setsockopt(zmq_sock, ZMQ_LINGER, &linger, sizeof linger);
>   rc = zmq_bind(zmq_sock, LISTEN_ADDRESS);
>
> Moving this to just before closing the socket doesn't help:
>
>   int linger = 0;
>   zmq_setsockopt(zmq_sock, ZMQ_LINGER, &linger, sizeof linger);
>   zmq_close(zmq_sock);
>
>
> ?
>
> Best regards
> Morten Møller Riis
>
>
>
> On Sep 24, 2013, at 5:14 PM, Pieter Hintjens <ph at imatix.com> wrote:
>
> This is a (rather poor) design choice in libzmq where the default
> linger on sockets is infinite, and the library does not properly
> handle socket shutdown. You've got two options. One, set linger=0 on
> the socket before closing it. Two, use CZMQ, which does this
> automatically on sockets.
>
> -Pieter
>
> On Mon, Sep 23, 2013 at 7:43 AM, Morten Møller Riis
> <mortenmoellerriis at gmail.com> wrote:
>
> Hi guys
>
> I'm having trouble catching SIGTERM and closing socket and context.
>
> I have the following code (I've trimmed it down since a lot of other stuff
> is happening). I use the global variable term_flag to break the main loop
> when the program receives SIGTERM.
>
> The problem is that when sending SIGTERM to the program it hangs at
> zmq_term(). Sending SIGTERM once more closes the program. Why is this
> happening?
>
>
> int term_flag = 1;
>
> void term(int signum)
> {
>    fprintf(log_fp, "Received %i, closing context and socket...\n", signum);
>    fprintf(log_fp, "Exiting.\n");
>    term_flag = 0;
> }
>
> int main(int argc, char **argv) {
>  ...
>
>  signal(SIGTERM, term);
>  signal(SIGINT, term);
>
>>
>  while(term_flag) {
>>    rc = zmq_recv(zmq_sock, &msg, 0);
>    if(rc!=0) break;
>>  }
>
>  zmq_close(zmq_sock);
>  zmq_term(zmq_ctx); // hangs here
>  ...
>  return 0;
> }
>
>
> Best regards
> Morten Møller Riis
>
>
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>



More information about the zeromq-dev mailing list