From 56b11bd4ca155726280df815dce480c173b1ceb0 Mon Sep 17 00:00:00 2001 From: Arman Hosseini Date: Mon, 28 Sep 2026 20:59:43 +0330 Subject: [PATCH] Fix possible data race while calling has_ready_entities in EventsCBGExecutor Signed-off-by: Arman Hosseini --- .../first_in_first_out_scheduler.cpp | 22 ++++++++++++------- .../events_cbg_executor/scheduler.hpp | 5 +++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.cpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.cpp index c758982c4c..dfd7bad2df 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.cpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/first_in_first_out_scheduler.cpp @@ -163,9 +163,13 @@ CBGScheduler::ExecutableEntityWithInfo FirstInFirstOutScheduler::get_next_ready_ std::optional ret = ready_cbg->get_next_ready_entity(); - if (ready_cbg->get_type() == CallbackGroupType::Reentrant && ready_cbg->has_ready_entities()) { - ready_callback_groups.push_back(ready_cbg); - ready_cbg->in_queue = true; + if (ready_cbg->get_type() == CallbackGroupType::Reentrant) { + std::lock_guard lock(ready_cbg->get_ready_mutex()); + + if (ready_cbg->has_ready_entities()) { + ready_callback_groups.push_back(ready_cbg); + ready_cbg->in_queue = true; + } } if(ret) { @@ -193,11 +197,13 @@ CBGScheduler::ExecutableEntityWithInfo FirstInFirstOutScheduler::get_next_ready_ ready_callback_groups.erase(it); ready_cbg->in_queue = false; - if ( - ready_cbg->get_type() == CallbackGroupType::Reentrant && ready_cbg->has_ready_entities()) - { - ready_callback_groups.push_back(ready_cbg); - ready_cbg->in_queue = true; + if (ready_cbg->get_type() == CallbackGroupType::Reentrant) { + std::lock_guard lock(ready_cbg->get_ready_mutex()); + + if (ready_cbg->has_ready_entities()) { + ready_callback_groups.push_back(ready_cbg); + ready_cbg->in_queue = true; + } } return CBGScheduler::ExecutableEntityWithInfo{ .entity = std::move(ret), .moreEntitiesReady = !ready_callback_groups.empty()}; diff --git a/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp b/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp index 606b330b24..31cea464b2 100644 --- a/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp +++ b/rclcpp/src/rclcpp/executors/events_cbg_executor/scheduler.hpp @@ -108,6 +108,11 @@ class CBGScheduler bool is_ready(); + std::mutex & get_ready_mutex() + { + return ready_mutex; + } + // true if this cbg is inside the scheduler's queue bool in_queue = false;