zeromq / libzmq

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

Assertion failed: socket_type >= 0 && socket_type < (int) names_count (src/mechanism.cpp:110) #4487

Open renukamanavalan opened 1 year ago

renukamanavalan commented 1 year ago

Issue description

Simple ZMQ model using ZMQ_CLIENT & ZMQ_SERVER fails with following assertion. Assertion failed: socket_type >= 0 && socket_type < (int) names_count (src/mechanism.cpp:110)

Code is pretty simple. Invoke it in client / server mode. Server: Creates sock, binds and sleep for an hour before exiting Client: Creates sock, connect & sleep

The assertion happens while the main thread is sleeping.

BTW, I saw it working for a while, before failing consistently.

Environment

# Minimal test code / Steps to reproduce the issue

include

include

include

include

include

include

const char *zmq_path = "tcp://127.0.0.1:5555";

int main(int argc, char *argv) { int opt; int run_server = 0; void zmq_ctx = NULL; void *zmq_sck = NULL; char buf[10];

while ((opt = getopt(argc, argv, "s")) != -1) {
    switch (opt) {
    case 's':
        run_server = 1;
        break;

    default: /* '?' */
        fprintf(stderr, "Usage: %s [-s]\n", argv[0]);
        exit(-1);
    }
}

zmq_ctx = zmq_ctx_new ();

if (run_server != 0) {
    zmq_sck = zmq_socket (zmq_ctx, ZMQ_SERVER);

    printf ("Binding to server %s\n", zmq_path);
    zmq_bind (zmq_sck, zmq_path);
    printf ("Bound to server %s\n", zmq_path);

}
else {
    zmq_sck = zmq_socket (zmq_ctx, ZMQ_CLIENT);

    printf ("connecting server %s\n", zmq_path);
    zmq_connect (zmq_sck, zmq_path);
    printf ("Connected to server %s\n", zmq_path);

}
printf("Sleeping\n");
sleep(3600);
printf("Closing socket...\n");
zmq_close (zmq_sck);
printf("Destroy context ...\n");
zmq_ctx_destroy (zmq_ctx);

}


# What's the actual result? (include assertion message & call stack if applicable)
As Server:
NOTE: Assertion is while sleeping

localadmin@remanava-dev-1:~/tools/zmq/c/client_server$ ./t -s Binding to server tcp://127.0.0.1:5555 Bound to server tcp://127.0.0.1:5555 Sleeping Assertion failed: socket_type >= 0 && socket_type < (int) names_count (src/mechanism.cpp:110) Aborted (core dumped) localadmin@remanava-dev-1:~/tools/zmq/c/client_server$


As client:
NOTE: Assertion is while sleeping - implying during async connect 

localadmin@remanava-dev-1:~/tools/zmq/c/client_server$ ./t connecting server tcp://127.0.0.1:5555 Connected to server tcp://127.0.0.1:5555 Sleeping Assertion failed: socket_type >= 0 && socket_type < (int) names_count (src/mechanism.cpp:110) Aborted (core dumped) localadmin@remanava-dev-1:~/tools/zmq/c/client_server$



# What's the expected result?
No assertion.
Connection to succeed.
ljluestc commented 3 days ago

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <zmq.h>

const char *zmq_path = "tcp://127.0.0.1:5555";

int main(int argc, char **argv)
{
    int opt;
    int run_server = 0;
    void *zmq_ctx = NULL;
    void *zmq_sck = NULL;

    // Parse command line arguments
    while ((opt = getopt(argc, argv, "s")) != -1) {
        switch (opt) {
        case 's':
            run_server = 1;
            break;

        default: /* '?' */
            fprintf(stderr, "Usage: %s [-s]\n", argv[0]);
            exit(-1);
        }
    }

    // Create a new ZeroMQ context
    zmq_ctx = zmq_ctx_new();

    if (run_server != 0) {
        // Server: Create a REP socket
        zmq_sck = zmq_socket(zmq_ctx, ZMQ_REP);
        printf("Binding to server %s\n", zmq_path);
        zmq_bind(zmq_sck, zmq_path);
        printf("Bound to server %s\n", zmq_path);

        // Wait for messages (for example purposes)
        while (1) {
            char buffer[256];
            zmq_recv(zmq_sck, buffer, sizeof(buffer), 0); // Receive a message
            printf("Received: %s\n", buffer); // Print the received message
            zmq_send(zmq_sck, "ACK", 3, 0); // Send an acknowledgment
        }
    }
    else {
        // Client: Create a REQ socket
        zmq_sck = zmq_socket(zmq_ctx, ZMQ_REQ);
        printf("Connecting to server %s\n", zmq_path);
        zmq_connect(zmq_sck, zmq_path);
        printf("Connected to server %s\n", zmq_path);

        // Send a message to the server
        zmq_send(zmq_sck, "Hello", 5, 0);
        char buffer[256];
        zmq_recv(zmq_sck, buffer, sizeof(buffer), 0); // Wait for acknowledgment
        printf("Received: %s\n", buffer); // Print acknowledgment
    }

    // Sleep for a while
    printf("Sleeping\n");
    sleep(3600);
    printf("Closing socket...\n");
    zmq_close(zmq_sck);
    printf("Destroying context ...\n");
    zmq_ctx_destroy(zmq_ctx);

    return 0;
}