From 98fc06e604d96e8b502ca2d84805bb0c3767c3b2 Mon Sep 17 00:00:00 2001 From: Vojtech Spurny Date: Mon, 5 Oct 2026 21:03:25 +0200 Subject: [PATCH] Drop queued events of removed callback groups in EventsCBGExecutor The scheduler keeps the CallbackGroupHandle of every callback group that was ever registered, together with its queued ReadyEntities, until the executor is destroyed. Events that were queued but not executed when a node was removed therefore hold weak pointers to its entities. In a component container the executor outlives the ComponentManager, so the weak pointers are released after the component library was unloaded and the process crashes with SIGSEGV on shutdown. Clear the queued events when the RegisteredEntityCache of a callback group is destroyed. The handle itself is kept, as worker threads may still reference it while executing an entity. Add a regression test, that removes a node while events of its waitable are queued. Signed-off-by: Vojtech Spurny Assisted-by: Claude Code:claude-opus-5-5 --- .../first_in_first_out_scheduler.hpp | 5 + .../registered_entity_cache.hpp | 8 ++ .../events_cbg_executor/scheduler.hpp | 15 +++ rclcpp/test/rclcpp/CMakeLists.txt | 8 ++ .../test_events_cbg_executor_remove_node.cpp | 111 ++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 rclcpp/test/rclcpp/executors/test_events_cbg_executor_remove_node.cpp diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.hpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.hpp index 52f5c072f0..6950efc9a3 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.hpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.hpp @@ -59,6 +59,11 @@ struct FirstInFirstOutCallbackGroupHandle final : public CBGScheduler::CallbackG return !ready_entities.empty(); } + void clear_ready_entities_intern() final + { + ready_entities.clear(); + } + private: std::deque ready_entities; }; diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/registered_entity_cache.hpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/registered_entity_cache.hpp index 7109ba647c..ed45cd4a09 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/registered_entity_cache.hpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/registered_entity_cache.hpp @@ -147,6 +147,14 @@ struct RegisteredEntityCache TimerManager & timer_manager; ~RegisteredEntityCache() + { + unregister_callbacks(); + + // drop events still queued in the scheduler + scheduler_cbg_handle.clear_ready_entities(); + } + + void unregister_callbacks() { for (const auto & gc_ref : guard_conditions) { gc_ref.guard_condition->set_on_trigger_callback(nullptr); diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp index 48a4cec116..221f8e495b 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp @@ -235,6 +235,16 @@ class CBGScheduler scheduler.callback_group_ready(this, false); } + /** + * Removes all pending ready entities. Must be called + * if the callback group is removed from the executor. + */ + void clear_ready_entities() + { + std::lock_guard l(ready_mutex); + clear_ready_entities_intern(); + } + CallbackGroupType get_type() {return type;} bool is_ready(); @@ -250,6 +260,11 @@ class CBGScheduler */ virtual bool has_ready_entities() const = 0; + /** + * Will always be called under lock of ready_mutex + */ + virtual void clear_ready_entities_intern() = 0; + /** * Executes the given function to add an entity * under the ready mutex. Afterwards the function diff --git a/rclcpp/test/rclcpp/CMakeLists.txt b/rclcpp/test/rclcpp/CMakeLists.txt index e43ce91ffe..11d12ff08e 100644 --- a/rclcpp/test/rclcpp/CMakeLists.txt +++ b/rclcpp/test/rclcpp/CMakeLists.txt @@ -555,6 +555,14 @@ if(TARGET test_events_cbg_executor_timer_removal) target_link_libraries(test_events_cbg_executor_timer_removal ${PROJECT_NAME}) endif() +ament_add_ros_isolated_gtest(test_events_cbg_executor_remove_node + executors/test_events_cbg_executor_remove_node.cpp + executors/test_waitable.cpp + APPEND_LIBRARY_DIRS "${append_library_dirs}") +if(TARGET test_events_cbg_executor_remove_node) + target_link_libraries(test_events_cbg_executor_remove_node ${PROJECT_NAME}) +endif() + ament_add_ros_isolated_gtest(test_entities_collector executors/test_entities_collector.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}" TIMEOUT 120) if(TARGET test_entities_collector) diff --git a/rclcpp/test/rclcpp/executors/test_events_cbg_executor_remove_node.cpp b/rclcpp/test/rclcpp/executors/test_events_cbg_executor_remove_node.cpp new file mode 100644 index 0000000000..425e166c46 --- /dev/null +++ b/rclcpp/test/rclcpp/executors/test_events_cbg_executor_remove_node.cpp @@ -0,0 +1,111 @@ +// Copyright 2026 Fly4Future +// Copyright 2026 Czech Technical University in Prague +// Copyright 2026 Vojtech Spurny +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +#include "rclcpp/rclcpp.hpp" + +#include "./test_waitable.hpp" + +class TestEventsCBGExecutorRemoveNode : public testing::Test +{ +protected: + static void SetUpTestCase() {rclcpp::init(0, nullptr);} + + static void TearDownTestCase() {rclcpp::shutdown();} +}; + +/* + * Allocator, that records when the memory of the shared_ptr control block + * is released. This only happens after the last weak_ptr is gone. + */ +template +struct ReleaseTrackingAllocator +{ + using value_type = T; + + explicit ReleaseTrackingAllocator(bool & released) + : released(released) {} + + template + ReleaseTrackingAllocator(const ReleaseTrackingAllocator & other) // NOLINT + : released(other.released) {} + + T * allocate(std::size_t n) + { + return std::allocator{}.allocate(n); + } + + void deallocate(T * p, std::size_t n) + { + released = true; + std::allocator{}.deallocate(p, n); + } + + template + bool operator==(const ReleaseTrackingAllocator & other) const + { + return &released == &other.released; + } + + template + bool operator!=(const ReleaseTrackingAllocator & other) const + { + return !(*this == other); + } + + bool & released; +}; + +/* + * Test that events, which were queued but not executed, do not keep the + * entities of a removed node alive until the executor is destroyed. + * + * In a component container the executor outlives the components, and their + * libraries are unloaded before the executor is destroyed. Releasing the + * last weak_ptr to an entity at that point calls into unloaded code. + */ +TEST_F(TestEventsCBGExecutorRemoveNode, queued_events_released_on_remove_node) +{ + bool released = false; + + auto executor = std::make_shared(); + + { + auto node = std::make_shared("test_events_cbg_executor_remove_node"); + auto cbg = node->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive); + + auto waitable = std::allocate_shared( + ReleaseTrackingAllocator(released)); + node->get_node_waitables_interface()->add_waitable(waitable, cbg); + + executor->add_node(node); + + // not spinning, so the events stay queued in the executor + waitable->trigger(); + waitable->trigger(); + + executor->remove_node(node); + + node->get_node_waitables_interface()->remove_waitable(waitable, cbg); + } + + // the executor is still alive here + EXPECT_TRUE(released); +}