zmq_close hanging on unused socket

Description

Connecting to a non existent host, then calling zmq_close causes a block

Sample code:

#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <stdbool.h> #include <signal.h> #include <string.h> #include <unistd.h> #include "zmq.h" static bool keepRunning = true; void sig_handler(int signo) { keepRunning = false; } int main ( int argc, char *argv[] ) { if (signal(SIGINT, sig_handler) == SIG_ERR) printf("\ncan't catch SIGINT\n"); void *context = zmq_ctx_new (); void *sender = zmq_socket (context, ZMQ_PUSH); uint64_t linger = 0; zmq_setsockopt( sender , ZMQ_LINGER, &linger, sizeof(linger)); int rc = zmq_connect (sender, "tcp://8.8.8.8:2120"); if (rc == -1) { printf ("E: bind failed: %s\n", strerror (errno)); return -1; } while(keepRunning) { printf("Send message\n"); zmq_send (sender, "test", 4, 0); sleep(1); } printf("Shutting down...\n"); zmq_close (sender); printf("Closed\n"); zmq_ctx_destroy (context); return 0; }

Where IP 8.8.8.8 is used to demonstrate a host blocking connections on port 2120.

Compiled like so:

gcc -Wall -o test -lzmq test.c

When I run this, I get the following output:

# ./test Send message Send message Send message Send message Send message ^CShutting down... <--- hit CTRL+C here Closed <---- blocking

Environment

Centos Linux 6.4, 64bit
kernel 2.6.32
glibc-2.12-1
libstdc++-4.4.7
zeromq-3.2.3

Activity

Show:

Guido G. August 29, 2013 at 10:07 AM

Might well be that the SO_LINGER option only works on a valid connection.
Which in this case never will happen.

Will have a look.

Details

Assignee

Reporter

Affects versions

Priority

Created June 20, 2013 at 3:00 PM
Updated August 29, 2013 at 10:07 AM