eclipse-paho / paho.mqtt.cpp

Other
1.03k stars 438 forks source link

ThreadSanitizer: lock-order-inversion (potential deadlock) paho 1.2.0 #481

Open Mikevvp opened 9 months ago

Mikevvp commented 9 months ago

Hi, Issue description: WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) When compiling sample https://github.com/eclipse/paho.mqtt.cpp/blob/master/src/samples/async_subscribe.cpp) with -fsanitize=thread and get a TSAN Sanitizer Warning. tsan_sanitizer_output.txt

Used:

This needs a fix i suppose

fpagliughi commented 3 months ago

Thanks for reporting this! Just for completeness, here is the report, simplified to make it easier to follow:

Output:
----------------------------------------------------------
Connecting to the MQTT server...
Disconnecting from the MQTT server...==================
WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=318824)
  Cycle in lock order graph: M0 (0x7ffce740e120) => M1 (0x560a45361a78) => M0

  Mutex M1 acquired here while holding mutex M0 in main thread:
    #0 pthread_mutex_lock <null> (module_test+0x18fe0b)
    #1 Thread_lock_mutex paho-mqtt-c/1.3.12_4/src/Thread.c:153:8 
    #2 MQTTAsync_lock_mutex paho-mqtt-c/1.3.12_4/src/MQTTAsyncUtils.c:168:11
    #3 MQTTAsync_setConnected paho-mqtt-c/1.3.12_4/src/MQTTAsync.c:1632:2
    #4 mqtt::async_client::set_callback(mqtt::callback&) paho-mqtt-cpp/1.2.0_4/src/async_client.cpp:368:11
    #5 main /workspaces/app/test/module/main.cpp:177:7

    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 to get more informative warning message

  Mutex M0 acquired here while holding mutex M1 in thread T2:
    #0 pthread_mutex_lock <null> 
    #1 __gthread_mutex_lock(pthread_mutex_t*) include/x86_64-linux-gnu/c++/9/bits/gthr-default.h:749:12 (module_test+0x248d73) (BuildId: 08b72590ae3a0871a29a04d5978c840179425683)
    #2 std::mutex::lock() include/c++/9/bits/std_mutex.h:100:17
    #3 std::unique_lock<std::mutex>::lock() include/c++/9/bits/unique_lock.h:141:17
    #4 std::unique_lock<std::mutex>::unique_lock(std::mutex&) include/c++/9/bits/unique_lock.h:71:2
    #5 mqtt::async_client::remove_token(mqtt::token*) paho-mqtt-cpp/1.2.0_4/src/async_client.cpp:331:8 
    #6 mqtt::token::on_success(MQTTAsync_successData*) paho-mqtt-cpp/1.2.0_4/src/token.cpp:126:8 
    #7 mqtt::token::on_success(void*, MQTTAsync_successData*) paho-mqtt-cpp/1.2.0_4/src/token.cpp:63:33 
    #8 MQTTAsync_receiveThread paho-mqtt-c/1.3.12_4/src/MQTTAsyncUtils.c:2097:8

  Thread T2 'MQTTAsync_rcv' (tid=318827, running) created by main thread at:
    #0 pthread_create <null>
    #1 Thread_start paho-mqtt-c/1.3.12_4/src/Thread.c:75:6
    #2 MQTTAsync_connect paho-mqtt-c/1.3.12_4/src/MQTTAsync.c:666:3
    #3 mqtt::async_client::connect(mqtt::connect_options, void*, mqtt::iaction_listener&) paho-mqtt-cpp/1.2.0_4/src/async_client.cpp:484:11
    #4 main /workspaces/app/test/module/main.cpp:184:9 

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) (/workspaces/app/build/Debug/Debug/module_test+0x18fe0b) in __interceptor_pthread_mutex_lock
==================

Connection success

Subscribing to topic 'hello'
    for client paho_cpp_async_subcribe using QoS1

Press Q<Enter> to quit

OK
Subscription failure for token: [1]

ThreadSanitizer: reported 1 warnings

Definitely looks like this should be fixed.