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 <[email protected]> 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 > [email protected] > http://lists.zeromq.org/mailman/listinfo/zeromq-dev > _______________________________________________ zeromq-dev mailing list [email protected] http://lists.zeromq.org/mailman/listinfo/zeromq-dev
