From 1e86c1884a9c4318795f7060753cda2baaac9288 Mon Sep 17 00:00:00 2001 From: Skyler Medeiros Date: Sun, 23 Aug 2026 16:16:37 -0700 Subject: [PATCH] Fix possible race condition with reentrant callback groups in EventsCBGExecutor scheduler (#3234) * put in_queue under ready_callback_groups_mutex Signed-off-by: Skyler Medeiros * only remove from ready_callback_groups if in the queue Signed-off-by: Skyler Medeiros --------- Signed-off-by: Skyler Medeiros Co-authored-by: Skyler Medeiros (cherry picked from commit 4528e3caef150eba5f2641fd290fd85796dbdc1a) --- .../executors/events_cbg_executor/scheduler.hpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp index e50bc1953c..cbdf4b50c7 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp @@ -13,6 +13,7 @@ // limitations under the License. #pragma once +#include #include #include #include @@ -214,8 +215,12 @@ class CBGScheduler void remove_callback_group(const CallbackGroupHandle *callback_handle) { std::lock_guard lk(ready_callback_groups_mutex); - ready_callback_groups.erase(std::find(ready_callback_groups.begin(), - ready_callback_groups.end(), callback_handle)); + + auto cbg_it = std::find(ready_callback_groups.begin(), + ready_callback_groups.end(), callback_handle); + if (cbg_it != ready_callback_groups.end()) { + ready_callback_groups.erase(cbg_it); + } callback_groups.remove_if([&callback_handle] (const auto & e) { return e.get() == callback_handle; @@ -230,11 +235,13 @@ class CBGScheduler */ void callback_group_ready(CallbackGroupHandle *handle, bool callback_group_was_idle) { - if (!handle->in_queue) { + { std::lock_guard l(ready_callback_groups_mutex); - ready_callback_groups.push_back(handle); - handle->in_queue = true; + if (!handle->in_queue) { + ready_callback_groups.push_back(handle); + handle->in_queue = true; + } } if(callback_group_was_idle) {