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

Jim Melton jim at melton.space
Wed Sep 28 04:37:45 CET 2022


That’s not a bug; it’s a feature. From http://api.zeromq.org/2-1%3azmq-connect <http://api.zeromq.org/2-1:zmq-connect>,

The connection will not be performed immediately but as needed by ØMQ. Thus a successful invocation of zmq_connect() does not indicate that a physical connection was or can actually be established.


ZMQ sockets are not TCP sockets. There is an unfortunate overloading of terms, and what you think you know may not apply to ZMQ. Go back and read https://zguide.zeromq.org/docs/chapter2/#The-Socket-API <https://zguide.zeromq.org/docs/chapter2/#The-Socket-API> where this is described in some detail.
--
Jim Melton




> On Sep 27, 2022, at 3:09 PM, Yuri <yuri at rawbw.com> wrote:
> 
> 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'.
> 
> 
> 
> Thanks,
> 
> Yuri
> 
> 
> 
> 
> 
> ---begin code---
> 
> import zmq
> 
> bogus_address = "1.2.3.4:17882"
> 
> 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...")
> socket.send_string("something")
> print("sent string")
> 
> print("waiting for acknowledgement")
> socket.recv(4096)
> print("acknowledgement arrived")
> 
> ---end code---
> 
> 
> 
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20220927/15ccd262/attachment.htm>


More information about the zeromq-dev mailing list