[zeromq-dev] Unreasonable behavior of a socket when connection can't be established. How to enable the blocking mode on a socket?

Yuri yuri at rawbw.com
Tue Sep 27 21:09:24 CET 2022

The code below successfully "connects" to a bogus host and then waits 
for response, when in reality the connect(2) call returned errno 36 
(Operation now in progress) and connection wasn't successful.

This behavior hides the real problem: inability to connect to a host.

The correct behavior for a non-blocking socket would be to wait for 
connection to complete or fail, but this doesn't happen for some reason.

The situation is worse when the host is changed to localhost, when it is 
known that this port is closed. This program still waits forever, when 
the correct behavior is to fail with 'Connection refused'.

It appears that libzmq sets the O_NONBLOCK option on all sockets, which 
causes such behavior.

How to change the socket back to the blocking mode such that 'connect' 
would behave the same way as the original POSIX connect(2) call would in 
the blocking mode?

I also believe that the behavior described above constitutes a bug in 
libzmq. The connect call should time out after some number of seconds 
and should fail with 'Timeout expired while trying to connect to xx' or 
'Connection refused'.



---begin code---

import zmq

bogus_address = ""

context = zmq.Context()
socket = context.socket(zmq.REQ)

print("connecting to bogus address: "+bogus_address)
res = socket.connect("tcp://%s" % (bogus_address))
print("connected to bogus address")

print("sending string...")
print("sent string")

print("waiting for acknowledgement")
print("acknowledgement arrived")

---end code---

More information about the zeromq-dev mailing list