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

quic demo Segmentation fault after pushlish+close #231

Open
gavinshao111 opened this issue Jun 6, 2024 · 0 comments
Open

quic demo Segmentation fault after pushlish+close #231

gavinshao111 opened this issue Jun 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@gavinshao111
Copy link

Describe the bug
Connect to broker, then publish message with qos 1, then call nng_mqtt_disconnect immediately, Segmentation fault happened.
If sleep 0.5s before nng_mqtt_disconnect, process shutdown normally.

Environment Details

  • NanoSDK/NNG version release 0.10.1

Additional context
code

call_result_t run() {

    int rv;

    debug("初始化 2024-05-06 19:31");
    if ((rv = nng_mqtt_quic_client_open_conf(m_sock, server_uri.c_str(), &config_user)) != 0) {
        std::stringstream ss;
        ss << "nng_mqtt_quic_client_open_conf return " << rv << ": " << nng_strerror(rv);
        return {false, ss.str()};
    }
    if (0 != nng_mqtt_quic_set_connect_cb(m_sock, connect_cb, nullptr) ||
            0 != nng_mqtt_quic_set_disconnect_cb(m_sock, disconnect_cb, nullptr) ||
            0 != nng_mqtt_quic_set_msg_recv_cb(m_sock, msg_recv_cb, nullptr) ||
            0 != nng_mqtt_quic_set_msg_send_cb(m_sock, msg_send_cb, nullptr)) {
        return {false, "set callback error"};
    }

    debug("连接到%s", server_uri.c_str());
    nng_msg *msg;
    rv = nng_mqtt_msg_alloc(&msg, 0);
    if (rv != 0) {
        std::stringstream ss;
        ss << "nng_mqtt_msg_alloc return " << rv << ": " << nng_strerror(rv);
        return {false, ss.str()};
    }
    nng_mqtt_msg_set_packet_type(msg, NNG_MQTT_CONNECT);
    nng_mqtt_msg_set_connect_proto_version(msg, 4);
    nng_mqtt_msg_set_connect_keep_alive(msg, 20);
    nng_mqtt_msg_set_connect_clean_session(msg, true);
    nng_mqtt_msg_set_connect_client_id(msg, client_id.c_str());
    if (username.length() > 0)
        nng_mqtt_msg_set_connect_user_name(msg, username.c_str());
    if (password.length() > 0)
        nng_mqtt_msg_set_connect_password(msg, password.c_str());

    debug("准备发送连接包,client id: %s, username: %s, password: %s", 
        client_id.c_str(), username.c_str(), password.c_str());
    rv = nng_sendmsg(*m_sock, msg, NNG_FLAG_NONBLOCK);
    debug("done");
    if (rv != 0) {
        std::stringstream ss;
        ss << "nng_sendmsg return " << rv << ": " << nng_strerror(rv);
        return {false, ss.str()};
    }


    std::cout << "input any string to continue...\n";
    std::string cmd;
    std::cin >> cmd;

    int rc;
    if ((rc = nng_mqtt_msg_alloc(&msg, 0)) != 0) {
        std::stringstream ss;
        ss << "nng_mqtt_msg_alloc return " << rc << ": " << nng_strerror(rc);
        return {false, ss.str()};
    }
    nng_mqtt_msg_set_packet_type(
            msg, NNG_MQTT_PUBLISH);
    nng_mqtt_msg_set_publish_dup(msg, 0);
    nng_mqtt_msg_set_publish_qos(msg, 1);
    nng_mqtt_msg_set_publish_retain(msg, 0);
    nng_mqtt_msg_set_publish_topic(
            msg, "/hello");
    nng_mqtt_msg_set_publish_payload(msg, (uint8_t*)"payload", 7);
    rc = nng_sendmsg(*m_sock, msg, NNG_FLAG_ALLOC);
    debug("publish nng_sendmsg return %d %s", rc, nng_strerror(rc));

    debug("*****************sleep 0.5s before disconnect");
    //std::this_thread::sleep_for(std::chrono::milliseconds(500)); // 若publish后立即关闭,程序崩溃

    debug("关闭");
    rv = nng_mqtt_disconnect(m_sock, 5, NULL); // 断网时调用阻塞15s后返回4
    //rv = nng_close(*m_sock); // 断网时调用阻塞25s后返回0
    debug("nng_close/nng_mqtt_disconnect return %d %s", rv, nng_strerror(rv));

    std::cout << "input any string to continue...\n";
    std::cin >> cmd;

    return {true};
}

core file stack

#0  ___pthread_mutex_lock (mutex=0xd) at ./nptl/pthread_mutex_lock.c:80
#1  0x00007f6c004162ad in nni_plat_mtx_lock () from /testdir/cpp/quic/NanoSDK-0.10.1/build/libnng.so.1
#2  0x00007f6c003fd7c6 in nni_aio_finish_error () from /testdir/cpp/quic/NanoSDK-0.10.1/build/libnng.so.1
#3  0x00007f6c0045c29b in mqtt_close_unack_msg_cb () from /testdir/cpp/quic/NanoSDK-0.10.1/build/libnng.so.1
#4  0x00007f6c003fffc4 in nni_id_map_foreach () from /testdir/cpp/quic/NanoSDK-0.10.1/build/libnng.so.1
#5  0x00007f6c0041cb13 in quic_mqtt_stream_close () from /testdir/cpp/quic/NanoSDK-0.10.1/build/libnng.so.1
#6  0x00007f6c0045caeb in quic_connection_cb () from /testdir/cpp/quic/NanoSDK-0.10.1/build/libnng.so.1
#7  0x00007f6c000c3b23 in QuicConnOnShutdownComplete () from /testdir/cpp/quic/NanoSDK-0.10.1/extern/msquic/build/bin/Release/libmsquic.so.2
#8  0x00007f6c000cbb78 in QuicConnDrainOperations () from /testdir/cpp/quic/NanoSDK-0.10.1/extern/msquic/build/bin/Release/libmsquic.so.2
#9  0x00007f6c000bb453 in QuicWorkerProcessConnection () from /testdir/cpp/quic/NanoSDK-0.10.1/extern/msquic/build/bin/Release/libmsquic.so.2
#10 0x00007f6c000bb850 in QuicWorkerLoop () from /testdir/cpp/quic/NanoSDK-0.10.1/extern/msquic/build/bin/Release/libmsquic.so.2
#11 0x00007f6c000e9a13 in CxPlatRunExecutionContexts () from /testdir/cpp/quic/NanoSDK-0.10.1/extern/msquic/build/bin/Release/libmsquic.so.2
#12 0x00007f6c000e9c10 in CxPlatWorkerThread () from /testdir/cpp/quic/NanoSDK-0.10.1/extern/msquic/build/bin/Release/libmsquic.so.2
#13 0x00007f6bffc81ac3 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
#14 0x00007f6bffd13850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
@JaylinYu JaylinYu added the bug Something isn't working label Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants