From 2a44deeeae7165220a6a3abbc3d57b0e32eab714 Mon Sep 17 00:00:00 2001 From: mini-1235 Date: Tue, 17 Nov 2026 00:27:12 +0800 Subject: [PATCH] Fix EventsCBGExecutor spin_some and spin_all(0) executing only one entity collect_and_execute_ready_events() computed the deadline as start + max_duration. With a max_duration of 0, which means no limit, the deadline had already passed, so spin_some() and spin_all(0) returned after the first executed entity. Treat 0 as no time limit, as Executor::spin_some_impl() does. An entity that was added after its callback group needed up to three spin_some() calls before it was served: executing the notify guard condition event requests the sync, executing the sync function only sets needs_callback_group_resync, and the resync ran at the start of the next call. If the executed work requested a resync, resync and collect once more instead of returning. Add a test that several ready waitables are all executed by one spin_some() and one spin_all(0), and enable the spin_some warm-up tests for the EventsCBGExecutor. Co-Authored-By: Claude Fable 5.2 Signed-off-by: mini-1235 --- .../events_cbg_executor.cpp | 12 +++- .../test/rclcpp/executors/test_executors.cpp | 62 +++++++++++++++++++ .../executors/test_executors_warmup.cpp | 10 +-- 3 files changed, 78 insertions(+), 6 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 845498af1a..81787ea01a 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 @@ -508,7 +508,9 @@ bool EventsCBGExecutor::collect_and_execute_ready_events( } const auto start = std::chrono::steady_clock::now(); - const auto end_time = start + max_duration; + // a max_duration of 0 means that there is no time limit + const auto end_time = max_duration == std::chrono::nanoseconds::zero() ? + std::chrono::steady_clock::time_point::max() : start + max_duration; auto cur_time = start; bool had_work = false; @@ -522,7 +524,13 @@ bool EventsCBGExecutor::collect_and_execute_ready_events( had_work = true; - if (!recollect_if_no_work_available) { + // If entities were added, the collected work contained the request to resync + // the callback groups, and not yet the events of the new entities. In this + // case we need to resync and collect once more, even if we shall only collect + // once. Otherwise the new entities would not be served before the next call. + if (!recollect_if_no_work_available && + !needs_callback_group_resync.load(std::memory_order_acquire)) + { // we are done return had_work; } diff --git a/rclcpp/test/rclcpp/executors/test_executors.cpp b/rclcpp/test/rclcpp/executors/test_executors.cpp index 9c73cd613e..854d54a5b9 100644 --- a/rclcpp/test/rclcpp/executors/test_executors.cpp +++ b/rclcpp/test/rclcpp/executors/test_executors.cpp @@ -659,6 +659,68 @@ TYPED_TEST(TestExecutors, spinSome) } } +// The purpose of this test is to check that a max_duration of 0 means "no limit": +// - spin_some() executes all the work that was ready when it was called +// - spin_all(0) executes work until none is left +TYPED_TEST(TestExecutors, spinSomeAndSpinAllWithoutMaxDuration) +{ + using ExecutorType = TypeParam; + + // Use an isolated callback group to avoid interference from any housekeeping + // items that may be in the default callback group of the node. + constexpr bool automatically_add_to_executor_with_node = false; + auto isolated_callback_group = this->node->create_callback_group( + rclcpp::CallbackGroupType::MutuallyExclusive, + automatically_add_to_executor_with_node); + + // The callbacks take some time, so that the clock has always advanced + // after the first one was executed. + auto slow_callback = []() { + std::this_thread::sleep_for(1ms); + }; + + constexpr size_t number_of_waitables = 5u; + auto waitable_interfaces = this->node->get_node_waitables_interface(); + std::vector> waitables; + for (size_t i = 0; i < number_of_waitables; ++i) { + waitables.push_back(std::make_shared()); + waitables.back()->set_on_execute_callback(slow_callback); + waitable_interfaces->add_waitable(waitables.back(), isolated_callback_group); + } + + auto number_of_waitables_executed = [&waitables]() { + size_t count = 0; + for (const auto & waitable : waitables) { + count += waitable->get_count(); + } + return count; + }; + + ExecutorType executor; + executor.add_callback_group(isolated_callback_group, this->node->get_node_base_interface()); + + for (const auto & waitable : waitables) { + waitable->trigger(); + } + executor.spin_some(); + EXPECT_EQ(number_of_waitables_executed(), number_of_waitables) + << "spin_some() without a max_duration did not execute all the ready waitables"; + + // The deprecated EventsExecutor rejects a max_duration of 0 in spin_all() + RCPPUTILS_DEPRECATION_WARNING_OFF_START + if (std::is_same()) { + return; + } + RCPPUTILS_DEPRECATION_WARNING_OFF_STOP + + for (const auto & waitable : waitables) { + waitable->trigger(); + } + executor.spin_all(0ns); + EXPECT_EQ(number_of_waitables_executed(), 2u * number_of_waitables) + << "spin_all() with a max_duration of 0 did not execute all the ready waitables"; +} + // The purpose of this test is to check that the ExecutorT.spin_some() method: // - does not continue executing after max_duration has elapsed TYPED_TEST(TestExecutorsStable, spinSomeMaxDuration) diff --git a/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp b/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp index 84199dbd95..9e03499db3 100644 --- a/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp +++ b/rclcpp/test/rclcpp/executors/test_executors_warmup.cpp @@ -152,12 +152,13 @@ TYPED_TEST(TestExecutorsWarmup, spin_some_doesnt_require_warmup) { using ExecutorType = TypeParam; - // TODO(alsora): currently only the events-executor passes this test. + // TODO(alsora): currently only the events based executors pass this test. // Enable single-threaded and multi-threaded executors // when https://github.com/ros2/rclcpp/pull/2595 gets merged RCPPUTILS_DEPRECATION_WARNING_OFF_START if ( - !std::is_same()) + !std::is_same() && + !std::is_same()) { GTEST_SKIP(); } @@ -199,12 +200,13 @@ TYPED_TEST(TestExecutorsWarmup, spin_some_doesnt_require_warmup_with_cbgroup) { using ExecutorType = TypeParam; - // TODO(alsora): currently only the events-executor passes this test. + // TODO(alsora): currently only the events based executors pass this test. // Enable single-threaded and multi-threaded executors // when https://github.com/ros2/rclcpp/pull/2595 gets merged RCPPUTILS_DEPRECATION_WARNING_OFF_START if ( - !std::is_same()) + !std::is_same() && + !std::is_same()) { GTEST_SKIP(); }