Heap corruption in rcl_node_type_cache_* when an executor thread finalizes an entity while another thread creates one on the same node
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
Hướng nghiên cứu
Start by tracing how rclcpp client creation and finalization call rcl_node_type_cache_register_type and rcl_node_type_cache_unregister_type, then inspect the wait set ownership handling described in the report. Reproduce the race with the supplied client creation loop and verify that init and fini on one node are synchronized; done when the reproducer runs without heap corruption.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
Required Info:
- Operating System:
- Ubuntu 24.04
- Installation type:
- binaries
- Version or commit hash:
- Jazzy: rclcpp 28.1.23, rcl 9.2.11
- DDS implementation:
- rmw_cyclonedds_cpp
- Client library (if applicable):
- rclcpp
Steps to reproduce issue
A single user thread creates and destroys clients on a node that a MultiThreadedExecutor is spinning. The user code never touches the node from more than one thread.
#include <chrono>
#include <cstdio>
#include <memory>
#include <thread>
#include <vector>
#include <rclcpp/rclcpp.hpp>
#include <std_srvs/srv/empty.hpp>
#include <std_srvs/srv/set_bool.hpp>
#include <std_srvs/srv/trigger.hpp>
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("type_cache_repro");
rclcpp::executors::MultiThreadedExecutor executor(rclcpp::ExecutorOptions(), 4);
executor.add_node(node);
std::thread spin_thread([&executor]() { executor.spin(); });
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60);
long iterations = 0;
while (std::chrono::steady_clock::now() < deadline) {
std::vector<rclcpp::ClientBase::SharedPtr> clients;
clients.push_back(node->create_client<std_srvs::srv::SetBool>("set_bool"));
clients.push_back(node->create_client<std_srvs::srv::Trigger>("trigger"));
clients.push_back(node->create_client<std_srvs::srv::Empty>("empty"));
clients.clear();
++iterations;
}
std::printf("no crash after %ld iterations\n", iterations);
executor.cancel();
spin_thread.join();
rclcpp::shutdown();
return 0;
}
cmake_minimum_required(VERSION 3.16)
project(type_cache_repro CXX)
set(CMAKE_CXX_STANDARD 17)
find_package(rclcpp REQUIRED)
find_package(std_srvs REQUIRED)
add_executable(repro repro.cpp)
target_link_libraries(repro rclcpp::rclcpp ${std_srvs_TARGETS})
Expected behavior
Runs for 60 s and prints no crash after N iterations.
Actual behavior
It crashes before the deadline. Backtrace (via libbackward.so):
#11 repro.cpp:31 in clear
#10 shared_ptr_base.h:199 in _M_release_last_use
#9 destroy<rclcpp::Client<std_srvs::srv::Empty> >
#8 librclcpp.so
#7 librclcpp.so
#6 librcl.so in rcl_client_fini
#5 librcl.so in rcl_node_type_cache_unregister_type
#4 type_description_interfaces__msg__TypeDescription__destroy
#3 type_description_interfaces__msg__TypeDescription__fini
#2 type_description_interfaces__msg__Field__Sequence__fini
#1 type_description_interfaces__msg__Field__fini
#0 rosidl_runtime_c__String__fini
Segmentation fault (Address not mapped to object [0x31])
In a larger application, the same race shows up as glibc's double free or corruption (!prev). There, the frames have the executor thread finalizing a client inside Executor::wait_for_work, while another thread runs create_client on the same node:
rclcpp::executors::MultiThreadedExecutor::run
rclcpp::Executor::get_next_executable
rclcpp::Executor::wait_for_work
~Client<std_srvs::srv::SetBool>
rcl_client_fini
rcl_node_type_cache_unregister_type
rcutils_hash_map_set
free -> abort
Additional information
WaitSetTemplate::wait()takes shared ownership of every entity for the duration of the wait (storage_acquire_ownerships()/RCPPUTILS_SCOPE_EXIT(storage_release_ownerships())). So when the user drops the last reference to a client while an executor thread is waiting, the executor thread runsrcl_client_finiwhen its wait returns. Otherwise the user's thread does. The user has no control over which thread finalizes the entity.rcl_client_init,rcl_client_fini(and the matching service, publisher and subscription calls) register and unregister the type in the node's type cache (rcl_node_type_cache_register_type/_unregister_type). Both write the node'srcutils_hash_mapwithout synchronization. rcl documents these functions asThread-Safe: No.- So rclcpp calls the init and fini functions concurrently on the same node, although rcl says they are not thread-safe, and the application cannot prevent it by serializing its own calls.
- Looking at the source, the rcl type cache code and the wait set ownership handling are unchanged on
rolling, so this probably also affects Kilted and Rolling. I have only run the reproducer on Jazzy. - Possible fixes: a per-node mutex in rclcpp held around every
rcl_*_init/rcl_*_finithat touches the type cache, or synchronization insidercl_node_type_cache_*.
- Ngôn ngữ chính
- C++
- Star
- 829
- Fork
- 587
- Merge trung bình
- 4 ngày 6 giờ
- Pull request đã merge (30 ngày)
- 30
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ros2/rclcpp
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Memory grows per callback group created and destroyed on long lived nodesCó thể đã có người làm @jwallace42 đã nhận 1 ngày trước. Đang mởbug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 28/100
ros2/rclcpp#3428 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
removing and immediately re-adding a node to the EventsCBGExecutor leaves its entities unregisteredCó thể đã có người làm @skyegalaxy đã nhận 10 ngày trước. Đang mởbug
ros2/rclcpp#3418 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Humble] Removing a node with more than one auto-added callback group leaves a dangling guard condition in the executorCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
ros2/rclcpp#3410 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Copy constructor exception leaks intra-process message allocationCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
bug
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 82/100
EchoTools/nevr-runtime#2912 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[SDL3pp] update to 0.12.0Đang mởcategory:port-update
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
bug-unconfirmed
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
libc++
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
llvm/llvm-project#246508 ·
Maintainer thường phản hồi trong vòng 1 ngày