Error recursive_mutex.hpp

asked 2020-01-06 20:00:40 -0500

mrrius gravatar image

updated 2020-01-09 20:39:48 -0500

Hello,

So, I am developing a node with action server adn several subscriver. I use this code to spin:

ros::Rate rate(100);
while((!stop_all && !reload) && ros::ok()){
    ros::spinOnce();
    rate.sleep();
}

Everything works fine before I add action server. Now, everytime I quit the program by breaking the loop above it gives me error:

ControllerNode: /usr/local/include/boost/thread/pthread/recursive_mutex.hpp:113: void boost::recursive_mutex::lock(): Assertion `!pthread_mutex_lock(&m)' failed.
[Controller-1] process has died [pid 11292, exit code -6, cmd /home/pi/rover_workspace_for_dat2019/devel/lib/rover_control/ControllerNode __name:=Controller __log:=/home/pi/.ros/log/5da9d89a-30eb-11ea-b5a4-c0ee404204c4/Controller-1.log].
log file: /home/pi/.ros/log/5da9d89a-30eb-11ea-b5a4-c0ee404204c4/Controller-1*.log

The error doesn't really affect the program because it only appears when the node is shutdown. Everything works fine before action server is added. I have shutdown the action server too using .shutdown() method but still doesn't solve it. What caused this and how to solve it?

UPDATE:

gdb backtrace message

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x754d9824 in __GI_abort () at abort.c:89
#2  0x754d11a4 in __assert_fail_base (fmt=0x6ffb6000 "\001", 
    assertion=0xd34d4 "!pthread_mutex_lock(&m)", 
    assertion@entry=0x2 <error: Cannot access memory at address 0x2>, file=0x7effeb74 " ", 
    file@entry=0x755e9078 <lock> "", line=113, line@entry=1878745088, 
    function=function@entry=0xd8990 <boost::recursive_mutex::lock()::__PRETTY_FUNCTION__> "void boost::recursive_mutex::lock()") at assert.c:92
#3  0x754d1280 in __GI___assert_fail (assertion=0x2 <error: Cannot access memory at address 0x2>, 
    file=0x755e9078 <lock> "", line=1878745088, 
    function=0xd8990 <boost::recursive_mutex::lock()::__PRETTY_FUNCTION__> "void boost::recursive_mutex::lock()") at assert.c:101
#4  0x00093b18 in boost::recursive_mutex::lock (this=0x131888)
    at /usr/local/include/boost/thread/pthread/recursive_mutex.hpp:113
#5  0x000a61e4 in boost::unique_lock<boost::recursive_mutex>::lock (this=0x7effed40)
    at /usr/local/include/boost/thread/lock_types.hpp:346
#6  0x76c7ef44 in ros::TopicManager::unadvertise(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::shared_ptr<ros::SubscriberCallbacks> const&) ()
   from /opt/ros/kinetic/lib/libroscpp.so
#7  0x76c6facc in ros::Publisher::Impl::unadvertise() () from /opt/ros/kinetic/lib/libroscpp.so
#8  0x76c6fbe8 in ros::Publisher::Impl::~Impl() () from /opt/ros/kinetic/lib/libroscpp.so
#9  0x76c70884 in boost::detail::sp_counted_impl_pd<ros::Publisher::Impl*, boost::detail::sp_ms_deleter<ros::Publisher::Impl> >::dispose() () from /opt/ros/kinetic/lib/libroscpp.so
edit retag flag offensive close merge delete

Comments

by breaking the loop

Do you mean changing one of the two flags or by using CTRL-C to end the node ? (Or is it simply the same for each situation ?)

It's propbably related to your action server, can you provide some code please ?

Delb gravatar image Delb  ( 2020-01-07 02:29:19 -0500 )edit

yes, I send a message to topic stop and change the boolean.

The code is quite complicated. But the flow is really simple.

I have a c++ thread that do some calculation. When the action server receive goal, it will notify this thread to wake up. This thread is the one who publish feedback.

I also have another thread that will check if the goal is finished based on the calculation. This thread will be the one who will setSucceed the action server. This thread also set a boolean finish.

Meanwhile, what the action server callback do are wake up the calculation thread and do a loop until boolean finish is true.

mrrius gravatar image mrrius  ( 2020-01-07 20:16:47 -0500 )edit

yes, I send a message to topic stop and change the boolean.

Do you get the same error when terminating the program with CTRL-C ?

Your code is probably not thread safe, you should make sure that the threads are finished before freeing the memory. Have a look at std::thread::join(). If it doesn't help for your issue you should show some code.

Delb gravatar image Delb  ( 2020-01-08 02:22:02 -0500 )edit

The weird thing is, this error only occur when I add action server. I have joined other threads too. I have also shutdown the action server like other subscriber. Yes, my node exit normally with ctrl+C. It is only when I quit via subscriber to STOP message that it error. When I send RELOAD topic, it doesn't give any error. The only difference is that STOP just exit the program.

mrrius gravatar image mrrius  ( 2020-01-09 20:35:42 -0500 )edit

The backtrace just confirm the fact that you are not exiting your node propperly, meaning that you try to use something that has been destroyed : error: Cannot access memory at address. If it only happens when you are setting the flag stop to true then search for the part where you destroy your objects, end your threads, free your memory etc.. You could add a breakpoint when setting the flag to true and run through your code line by line to find where the error occurs.

I can't help you more without seeing any code.

Delb gravatar image Delb  ( 2020-01-10 07:35:37 -0500 )edit