erickt / rust-zmq

Rust zeromq bindings.
Apache License 2.0
886 stars 189 forks source link

Example in README.md hangs #369

Closed bkolligs closed 1 year ago

bkolligs commented 1 year ago

This code seems to hang for me after the send; any ideas why? I would expect this to throw an zmq::Error::EAGAIN and completely exit if the socket URL is not already bound.

fn main() {
    let ctx = zmq::Context::new();

    let socket = ctx.socket(zmq::REQ).unwrap();
    socket.connect("tcp://127.0.0.1:1234").unwrap();
    socket.send("hello world!", zmq::DONTWAIT).unwrap();
    println!("Still hanging");
    socket.recv_multipart(zmq::DONTWAIT).unwrap();
    println!("Hanging again");
}

rustc 1.68 Ubuntu 22

This is the corresponding python code:

import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:1234")
socket.send_string("Hello world!", flags=0)
socket.recv_string(flags=zmq.DONTWAIT)

Which produces the error:

Traceback (most recent call last):
  File "/home/user/zmq-test/test.py", line 6, in <module>
    socket.recv_string(flags=zmq.DONTWAIT)
  File "/home/user/venv/lib/python3.9/site-packages/zmq/sugar/socket.py", line 858, in recv_string
    msg = self.recv(flags=flags)
  File "zmq/backend/cython/socket.pyx", line 809, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 845, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 199, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/socket.pyx", line 194, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/checkrc.pxd", line 22, in zmq.backend.cython.checkrc._check_rc
zmq.error.Again: Resource temporarily unavailable
bkolligs commented 1 year ago

Not a bug, it is due to this: https://github.com/zeromq/pyzmq/issues/102. Fix is to manually set socket.set_linger(0) to avoid waiting indefinitely.