-
Notifications
You must be signed in to change notification settings - Fork 527
fix(executor): wake executor when future is set by non-executor thread (#1916) #3126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: rolling
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| #include <memory> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <thread> | ||
| #include <vector> | ||
|
|
||
| #include "rcl/guard_condition.h" | ||
|
|
@@ -375,22 +376,49 @@ class Executor | |
| * If the time spent inside the blocking loop exceeds this timeout, return a TIMEOUT return | ||
| * code. | ||
| * \return The return code, one of `SUCCESS`, `INTERRUPTED`, or `TIMEOUT`. | ||
| * \note This method will check the future and the timeout only when the executor is woken up. | ||
| * If this future is unrelated to an executor's entity, this method will not correctly detect | ||
| * when it's completed and therefore may wait forever and never time out. | ||
| */ | ||
| template<typename FutureT, typename TimeRepT = int64_t, typename TimeT = std::milli> | ||
| FutureReturnCode | ||
| spin_until_future_complete( | ||
| const FutureT & future, | ||
| std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1)) | ||
| { | ||
| // When a future is fulfilled by a non-executor thread, spin_until_future_complete_impl | ||
| // blocks indefinitely in wait_for_work() because nothing in the wait set becomes ready. | ||
| // A watcher thread triggers interrupt_guard_condition_ the moment the future is set, | ||
| // immediately waking the executor. See ros2/rclcpp#1916. | ||
| std::atomic<bool> stop_watcher{false}; | ||
| // Copy the shared_ptr so the watcher keeps the guard condition alive | ||
| // even if this executor is destroyed before the watcher exits. | ||
| std::shared_ptr<rclcpp::GuardCondition> watcher_gc = interrupt_guard_condition_; | ||
|
|
||
| std::thread watcher_thread( | ||
| [&future, &stop_watcher, watcher_gc]() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. watcher captures &future by reference, i think that this is going to be a lifetime concern? If the caller's future object is destroyed before the watcher thread finishes, this is undefined behavior? |
||
| // Use wait_for with a short interval rather than wait() so the thread | ||
| // can observe stop_watcher after spin_until_future_complete returns | ||
| // (on timeout or cancel), enabling a clean join() without blocking. | ||
| while (!stop_watcher.load(std::memory_order_relaxed)) { | ||
| if (future.wait_for(std::chrono::milliseconds(100)) == | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100ms polling interval is arbitrary and introduces worst-case latency. if the future becomes ready 1ms after a wait_for call starts, the watcher won't notice for up to 99ms. |
||
| std::future_status::ready) | ||
| { | ||
| try { | ||
| watcher_gc->trigger(); | ||
| } catch (const std::exception &) {} | ||
| return; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| RCPPUTILS_SCOPE_EXIT( | ||
| stop_watcher.store(true, std::memory_order_relaxed); | ||
| watcher_thread.join(); | ||
| ); | ||
|
|
||
| return spin_until_future_complete_impl( | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(timeout), | ||
| [&future](std::chrono::nanoseconds wait_time) { | ||
| return future.wait_for(wait_time); | ||
| } | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| /// Cancel any running spin* function, causing it to return. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a thread per spin call is a heavy mechanism. every invocation of spin_until_future_complete now spawns and joins an OS thread. in code that calls this repeatedly (e.g., in a loop sending service requests), that's a lot of thread creation/destruction overhead... i believe this generates the other problems.