From 3e2ccd8f5211fee4937c1eb53601b7d0f110c302 Mon Sep 17 00:00:00 2001 From: suryakanth Date: Wed, 2 Dec 2026 00:01:19 +0530 Subject: [PATCH] Fix re-adding a node to EventsCBGExecutor after remove_node Fixes #3418. Signed-off-by: suryakanth --- .../events_cbg_executor.cpp | 34 +++++++++++-------- ...test_events_cbg_executor_timer_removal.cpp | 21 ++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp index 81787ea01a..fb9b9a79b6 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/events_cbg_executor.cpp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -253,7 +254,8 @@ void EventsCBGExecutor::trigger_callback_group_sync() if (!spinning) { sync_callback_groups(); } else { - scheduler->unblock_one_worker_thread(); + // Make pending work visible to workers about to block. + scheduler->trigger_sync(); } } @@ -819,24 +821,26 @@ EventsCBGExecutor::remove_node( } { - std::lock_guard lock{added_nodes_mutex_}; - added_nodes.erase( - std::remove_if( - added_nodes.begin(), added_nodes.end(), [&node_ptr](const auto & weak_ptr) { - const auto shr_ptr = weak_ptr.lock(); - return shr_ptr && shr_ptr == node_ptr; - }), added_nodes.end()); - } - - node_ptr->for_each_callback_group( - [this](const rclcpp::CallbackGroup::SharedPtr & cbg) + std::lock_guard groups_lock{callback_groups_mutex}; { - unregister_event_callbacks(cbg); + std::lock_guard nodes_lock{added_nodes_mutex_}; + std::erase_if(added_nodes, [&node_ptr](const auto & weak_ptr) { + return weak_ptr.lock() == node_ptr; + }); } - ); - node_ptr->get_shared_notify_guard_condition()->set_on_trigger_callback( + // A remove and re-add can be coalesced into a single sync. Discard the old + // registrations now so that sync registers the node's entities again. + std::erase_if( + callback_groups, [&node_ptr](const CallbackGroupData & data) { + const auto group = data.callback_group.lock(); + return group && data.origin == CallbackGroupData::Origin::Node && + node_ptr->callback_group_in_node(group); + }); + + node_ptr->get_shared_notify_guard_condition()->set_on_trigger_callback( std::function()); + } trigger_callback_group_sync(); diff --git a/rclcpp/test/rclcpp/executors/test_events_cbg_executor_timer_removal.cpp b/rclcpp/test/rclcpp/executors/test_events_cbg_executor_timer_removal.cpp index 5aa7c21316..2b0033e863 100644 --- a/rclcpp/test/rclcpp/executors/test_events_cbg_executor_timer_removal.cpp +++ b/rclcpp/test/rclcpp/executors/test_events_cbg_executor_timer_removal.cpp @@ -183,3 +183,24 @@ TEST_F(TestEventsCBGExecutorTimerRemoval, remove_and_readd_node_during_spin) } EXPECT_GE(calls, calls_after_return + 10); } + +TEST_F(TestEventsCBGExecutorTimerRemoval, readd_node_before_callback_groups_sync) +{ + timer->cancel(); + int readded_timer_calls = 0; + auto readded_timer = node->create_wall_timer( + 1ms, [this, &readded_timer_calls]() { + if (++readded_timer_calls == 1) { + executor->remove_node(node); + executor->add_node(node); + } + }); + + // spin_some handles both operations in the same callback before it can sync again. + const auto deadline = std::chrono::steady_clock::now() + 2s; + while (readded_timer_calls < 2 && std::chrono::steady_clock::now() < deadline) { + executor->spin_some(5ms); + std::this_thread::sleep_for(1ms); + } + EXPECT_GE(readded_timer_calls, 2); +}