zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.8k stars 2.36k forks source link

Issue with ZMQ_RECONNECT_STOP_AFTER_DISCONNECT option #4720

Closed githejie closed 3 months ago

githejie commented 3 months ago

Issue description

The description of ZMQ_RECONNECT_STOP_AFTER_DISCONNECT in the zeromq documentation is: The 'ZMQ_RECONNECT_STOP_AFTER_DISCONNECT' option will stop reconnection when zmq_disconnect() has been called. This can be useful when the user's request failed (server not ready), as the socket does not need to continue to reconnect after user disconnect actively. (https://zeromq.github.io/libzmq/zmq_setsockopt.html)

However, in actual testing, I found that after the socket set the ZMQ_RECONNECT_STOP_AFTER_DISCONNECT option, there is a probability that the reconnection will be abandoned before calling zmq_disconnect.

int stopReconnectOnError = ZMQ_RECONNECT_STOP_AFTER_DISCONNECT;
zmq_setsockopt (sock, ZMQ_RECONNECT_STOP, &stopReconnectOnError, sizeof (stopReconnectOnError));
zmq_connect (sub, "ipc://@dummy");
print_events (sub_mon, 2 * 1000, 10);
zmq_disconnect (sub, "ipc://@dummy");
print_events (sub_mon, 2 * 1000, 10);

For the test code above, we expect to see output like this:

Got event: CLOSED
Got event: CONNECT_RETRIED
Got event: CLOSED
Got event: CONNECT_RETRIED
Got event: CLOSED
Got event: CONNECT_RETRIED
Got event: CLOSED
Got event: CONNECT_RETRIED
Got event: CLOSED
Got event: CLOSED
Still waiting for monitor event after 250 ms
Still waiting for monitor event after 500 ms
Still waiting for monitor event after 750 ms
Still waiting for monitor event after 1000 ms

When this problem occurs, the output is like this:

Got event: CLOSED
Still waiting for monitor event after 250 ms
Still waiting for monitor event after 500 ms
Still waiting for monitor event after 750 ms
Still waiting for monitor event after 1000 ms