ros / ros_comm

ROS communications-related packages, including core client libraries (roscpp, rospy, roslisp) and graph introspection tools (rostopic, rosnode, rosservice, rosparam).
http://wiki.ros.org/ros_comm
752 stars 913 forks source link

I got lock-order-inversion with ThreadSanitizer #2220

Open SdKay opened 2 years ago

SdKay commented 2 years ago

Ubuntu18.04 melodic my code, I want to check if there is a node with same name before I init

#include <ros/ros.h>
#include <stdio.h>

#define LOG() printf("[%d]\n", __LINE__)

int main(int argc, char ** argv)
{
    std::string node_name = "main";
    LOG();

    //init for check
    ros::init(argc, argv, "pre_check", ros::init_options::AnonymousName);

    std::vector<std::string> nodes;
    ros::master::getNodes(nodes);
    for(auto & node : nodes) {
        if("/" + node_name == node) { //check if another node with name "node_name" exists
            ros::shutdown();
            LOG();
            return -1;
        }
    }
    LOG();
    ros::shutdown();
    LOG();
    ros::waitForShutdown();
    LOG();

    //start work node
    ros::init(argc, argv, node_name);

    //do some work

    ros::spin();
}

build and run with LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtsan.so.0, i got this

==================
WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=4284)
  Cycle in lock order graph: M442 (0x7f49eae620a8) => M445 (0x7f49eae62148) => M442

  Mutex M445 acquired here while holding mutex M442 in main thread:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x532b4)
    #1 log4cxx::helpers::synchronized::synchronized(log4cxx::helpers::Mutex const&) <null> (liblog4cxx.so.10+0x14acdb)

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

  Mutex M442 acquired here while holding mutex M445 in main thread:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x532b4)
    #1 log4cxx::helpers::synchronized::synchronized(log4cxx::helpers::Mutex const&) <null> (liblog4cxx.so.10+0x14acdb)

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) (/usr/lib/x86_64-linux-gnu/libtsan.so.0+0x532b4) in __interceptor_pthread_mutex_lock
==================
[9]
[23]
==================
WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=4284)
  Cycle in lock order graph: M445 (0x7f49eae62148) => M442 (0x7f49eae620a8) => M445

  Mutex M442 acquired here while holding mutex M445 in main thread:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x532b4)
    #1 log4cxx::helpers::synchronized::synchronized(log4cxx::helpers::Mutex const&) <null> (liblog4cxx.so.10+0x14acdb)

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

  Mutex M445 acquired here while holding mutex M442 in main thread:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x532b4)
    #1 log4cxx::helpers::synchronized::synchronized(log4cxx::helpers::Mutex const&) <null> (liblog4cxx.so.10+0x14acdb)

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) (/usr/lib/x86_64-linux-gnu/libtsan.so.0+0x532b4) in __interceptor_pthread_mutex_lock
==================
[25]
[27]

when Ctrl+c, out put this:

WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=4284)
  Cycle in lock order graph: M567 (0x7b4400000228) => M581 (0x7b48000002b0) => M567

  Mutex M581 acquired here while holding mutex M567 in main thread:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x532b4)
    #1 ros::PollManager::addPollThreadListener(boost::function<void ()> const&) <null> (libroscpp.so+0xd46c2)

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

  Mutex M567 acquired here while holding mutex M581 in thread T1:
    #0 pthread_mutex_lock <null> (libtsan.so.0+0x532b4)
    #1 boost::unique_lock<boost::mutex>::lock() <null> (libroscpp.so+0x9fd87)

  Thread T1 (tid=4288, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x5fe84)
    #1 boost::thread::start_thread_noexcept() <null> (libboost_thread.so.1.65.1+0x10b09)

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) (/usr/lib/x86_64-linux-gnu/libtsan.so.0+0x532b4) in __interceptor_pthread_mutex_lock
==================
ThreadSanitizer: reported 3 warnings