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

Calling rosbag2_cpp::Reader::open from multiple threads leads to crashes #1925

Open
MaxFleur opened this issue Feb 27, 2025 · 0 comments
Open
Labels
bug Something isn't working

Comments

@MaxFleur
Copy link

MaxFleur commented Feb 27, 2025

Description

I am writing an application where users can edit bag files to create a new, edited bag file. This includes a feature to rename topics in the original bag file.
To rename topics, a rosbag2_cpp::Reader instance is used. This will open the original bag file, extract its metadata and rename the topic inside it. An additional instance of rosbag2_cpp::Writer then creates the renamed topic and later writes read messages into the new, edited bag file.
To speed things up, I've parallelized the algorithm to use a separate thread for each original bag topic. So, for example, if the original bag has four topics, four threads are used.
However, I realized that if the function rosbag2_cpp::Reader::open is called in multiple threads simultaneously, this will lead to crashes. Enclosing this code in a mutex will resolve the problem, though.
I've included a minimal project as well as an example bag file for reproducing the error. This minimum project just opens the bag file in multiple threads simultaneously.

Note: I am well aware that it might be unusual to call rosbag2_cpp::Reader::open multiple times simultaneously and that it's not a bug, but rather intended to be called sequentially. But I do not see a reason why it should not be possible to open this bag file multiple times for read only access. Maybe it is a problem of the underlying storage format?

Expected Behavior

The bag file is opened and read by different threads at once.

Actual Behavior

Sometimes the executed function leads to crashes.

To Reproduce

  1. Copy the provided "thread_test" dir to your ros2_workspace/src.
  2. Copy the provided bagfile to an arbitrary location.
  3. Adjust line 11 in main.cpp to include the bag file location.
  4. cd path/to/ros2_workspace
  5. colcon build
  6. source install/setup.bash
  7. ros2 run thread_test thread_test
  8. To reproduce the error, comment out line 19 and 21 in main.cpp so that the mutex is not used. It might take a few approaches until this error is triggered.

System

  • Ubuntu 22.04
  • ROS 2 Distro: Humble
  • Install Method: APT
  • Version: 0.10.0-1jammy.20250211.160146

Additional context

Backtrace:

#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=140737310099008) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=140737310099008) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=140737310099008, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007ffff6c42476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007ffff6c287f3 in __GI_abort () at ./stdlib/abort.c:79
#5  0x00007ffff70b042a in __gnu_cxx::__verbose_terminate_handler() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff70ae20c in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff70ae277 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007ffff70ae4d8 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007ffff7f55100 in ?? () from /opt/ros/humble/lib/librosbag2_cpp.so
#10 0x00007ffff7f683e5 in rosbag2_cpp::readers::SequentialReader::open(rosbag2_storage::StorageOptions const&, rosbag2_cpp::ConverterOptions const&) () from /opt/ros/humble/lib/librosbag2_cpp.so
#11 0x00007ffff7f65604 in rosbag2_cpp::Reader::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /opt/ros/humble/lib/librosbag2_cpp.so
#12 0x0000555555557b81 in main::$_1::operator()() const ()
#13 0x0000555555557af9 in main::$_0::operator()() const ()
#14 0x0000555555557ad5 in void std::__invoke_impl<void, main::$_0>(std::__invoke_other, main::$_0&&) ()
#15 0x0000555555557a95 in std::__invoke_result<main::$_0>::type std::__invoke<main::$_0>(main::$_0&&) ()
#16 0x0000555555557a6d in void std::thread::_Invoker<std::tuple<main::$_0> >::_M_invoke<0ul>(std::_Index_tuple<0ul>) ()
#17 0x0000555555557a45 in std::thread::_Invoker<std::tuple<main::$_0> >::operator()() ()
#18 0x0000555555557999 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<main::$_0> > >::_M_run() ()
#19 0x00007ffff70dc253 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#20 0x00007ffff6c94ac3 in start_thread (arg=<optimised out>) at ./nptl/pthread_create.c:442
#21 0x00007ffff6d26850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81

thread_test.tar.gz
dummy_bag.tar.gz

@MaxFleur MaxFleur added the bug Something isn't working label Feb 27, 2025
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

1 participant