How to cleanly close zeromq DEALER / ROUTER inproc connections

In one of my applications, I use inproc connections for DEALER / ROUTER. I set the linger option on the DEALER jack to 0, so all messages sent to the DEALER jack should be reset after closing the ROUTER jack. Although this works well for TCP connections, it blocks forproc. Here is a minimal working example:

#include <zmq.h>

#include <windows.h>

int main()
{
    void *context = zmq_ctx_new();

    void *router = zmq_socket(context, ZMQ_ROUTER);
    zmq_bind(router, "inproc://socket");

    void *dealer = zmq_socket(context, ZMQ_DEALER);
    zmq_connect(dealer, "inproc://socket");

    int linger = 0;
    zmq_setsockopt(dealer, ZMQ_LINGER, &linger, sizeof(linger));

    zmq_close(router);

    // sleep for 1 ms
    Sleep(1);

    // this call blocks
    zmq_send(dealer, "message", 7, 0);

    zmq_close(dealer);
    zmq_ctx_destroy(context);

    return 0;
}

Before the DEALER connector can be closed, it blocks the call to zmq_send (). In this minimal example, I had to add a call to Sleep (1). When this call is omitted, zmq_send () is not blocked. When blocked, the call stack looks like this:

[External Code] 
libzmq.dll!zmq::signaler_t::wait(int timeout_) Line 253 C++
libzmq.dll!zmq::mailbox_t::recv(zmq::command_t * cmd_, int timeout_) Line 80    C++
libzmq.dll!zmq::socket_base_t::process_commands(int timeout_, bool throttle_) Line 1023 C++
libzmq.dll!zmq::socket_base_t::send(zmq::msg_t * msg_, int flags_) Line 869 C++
libzmq.dll!s_sendmsg(zmq::socket_base_t * s_, zmq_msg_t * msg_, int flags_) Line 346    C++
libzmq.dll!zmq_send(void * s_, const void * buf_, unsigned __int64 len_, int flags_) Line 371   C++

Windows 10 x64, libzmq 4.2.1 ( 4.1.6) Visual Studio 2015. DEALER/ROUTER? libzmq?

+4
1

ZMQ_DONTWAIT

zmq_send()

?

// this call blocks no more
zmq_send(dealer, "message", 7, ZMQ_DONTWAIT);
int ec = zmq_errno();
printf("code: %d\nstring: %s\n", ec, zmq_strerror(ec));

code: 11

:

? .

+1

Source: https://habr.com/ru/post/1669359/


All Articles