Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug that the router may block forever when the sending queue is full #4665

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ZhuXxxx
Copy link

@ZhuXxxx ZhuXxxx commented Mar 11, 2024

If we create a router, and ZMQ_ROUTER_MANDATORY is set to 1, ZMQ_SNDTIMEO is set to -1(default value is -1, infinite),when a peer SNDHWM is reached, we send a message to this peer, at this time the peer disconnected, We will observe that this situation will cause the function to be blocked forever.

We can find this bug through the following code:

static void test_dealer(void* const context)
{   
    void* const dealer = zmq_socket(context, ZMQ_DEALER);
    //
    int rc = zmq_setsockopt(dealer, ZMQ_ROUTING_ID, "X", 1);
    assert(0 == rc);
    rc = zmq_connect(dealer, "inproc://a");
    assert(0 == rc);
    //
    ::std::this_thread::sleep_for(::std::chrono::seconds(5));
    zmq_close(dealer);
    //
    printf("close dealer\n");
}
int main(void)
{
    void* const context = zmq_ctx_new();
    //  
    void* const router = zmq_socket(context, ZMQ_ROUTER);
    //  
    const int on = 1;
    int rc = zmq_setsockopt(router, ZMQ_ROUTER_MANDATORY, &on, sizeof(on));
    assert(0 == rc);
    //  
    rc = zmq_bind(router, "inproc://a");
    assert(0 == rc);
    //  
    ::std::thread thread0(test_dealer, context);
    thread0.detach();
    ::std::this_thread::sleep_for(::std::chrono::seconds(1));
    //  
    while (1) {
        rc = zmq_send(router, "X", 1, ZMQ_SNDMORE);
        if (1 != rc) {
            break;
        }
        rc = zmq_send(router, "hello", 5, 0);
        if (5 != rc) {
            break;
        }
    }
    return 0;
}

@ZhuXxxx ZhuXxxx closed this Mar 11, 2024
@ZhuXxxx ZhuXxxx reopened this Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant