Skip to content

Let threadmanager::wait_for(stop_token, timeout) suspend as well - #8226

Open
hkaiser wants to merge 1 commit into
masterfrom
threadmanager-wait-stop-token
Open

hkaiser wants to merge 1 commit into
masterfrom
threadmanager-wait-stop-token

Conversation

@hkaiser

@hkaiser hkaiser commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

#8189 made threadmanager::wait() and wait_for(timeout) suspend an HPX thread for 100 microseconds between the checks while other work exists, so that a waiter with a high priority doesn't starve the threads it waits for on a single worker. wait_for(stop_token, timeout), which hpx::local::termination_detection(stop_token, timeout) uses, was left yielding: a high priority waiter on one worker kept being picked again until its timeout passed, and then returned false although the threads it waited for would have finished at once.

This change routes wait_for(stop_token, timeout) through the same wait as the other two. The wait now takes a callable that is checked on each round and stops the wait (returning false) once stop was requested on the token, as before.

The new test termination_detection_priority waits with a stop token from a high priority thread on one worker. Before this change it hit its 10 s timeout and failed; with it, it passes in about 30-50 ms.

Testing: GCC 12 and Clang 23 (warnings as errors), termination_detection_priority with 1 and 2 workers and termination_detection with 4 workers pass with both compilers; without the fix the new test fails with 1 worker.

wait() and wait_for(timeout) suspend an HPX thread for a short time while
other work exists, so that a waiter with a high priority doesn't starve the
threads it waits for on a single worker. wait_for(stop_token, timeout), which
hpx::local::termination_detection(stop_token, timeout) uses, still yielded:
a high priority waiter on one worker kept running until its timeout, and
returned false although the threads it waited for would have finished at
once. It now uses the same wait, which checks the stop token on each round.

The test termination_detection_priority waits from a high priority thread on
one worker; it hit the 10 s timeout before this change.

Signed-off-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Timed runtime waits can now finish when a stop is requested, rather than waiting until the timeout expires.
  • Tests
    • Added coverage for termination detection with a single worker, verifying that queued tasks run and the runtime stops successfully.

Threadmanager wait operations now pass stop-request predicates to the busy-wait helper. A new one-worker unit test checks termination detection after the waiter posts 20 tasks.

Changes: Threadmanager wait behavior

Layer / File(s) Summary
Pass stop requests through wait operations
libs/core/threadmanager/include/hpx/threadmanager/threadmanager.hpp, libs/core/threadmanager/src/threadmanager.cpp, libs/core/threadmanager/CMakeLists.txt
The stop-token overload delegates to wait_for_impl with a stop-request callback. The busy-wait helper checks the predicate. Other wait paths pass a predicate that never requests stopping. The module adds its hpx_functional dependency.
Check termination detection with one worker
libs/core/runtime_local/tests/unit/CMakeLists.txt, libs/core/runtime_local/tests/unit/termination_detection_priority.cpp
The unit test uses one worker and checks successful runtime start and stop, termination detection completion, and execution of all 20 posted tasks.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Possibly related issues

  • TheHPXProject/hpx#8074: The tracking issue describes threadmanager wait starvation on one worker. This PR addresses the same behavior for the stop-token overload of threadmanager::wait_for.

Suggested reviewers: divyanshitalreja12, v4xsh

Merge Risk: Minimal · up to f32a6

The change makes the stop-token wait suspend like the other wait paths, so a high-priority waiter no longer starves the threads it is waiting on. A regression test covers the one-worker case. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: threadmanager::wait_for(stop_token, timeout) will suspend instead of yielding, matching the raw summary, which shows wait_for delegating to a shared wait_for_impl that…
Description check Passed The description states the problem (the stop_token overload of wait_for kept yielding, starving a high-priority waiter on one worker), the fix (routing it through the same suspending wait with a calla…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: threadmanager::wait_for(stop_token, timeout) will suspend instead of yielding, matching the raw summary, which shows wait_for delegating to a shared wait_for_impl that yields the thread between checks and returns false on stop. It is specific and concise, and it is consistent with the PR objectives.

Full details: Description check

Explanation

The description states the problem (the stop_token overload of wait_for kept yielding, starving a high-priority waiter on one worker), the fix (routing it through the same suspending wait with a callable checked each round), and the new termination_detection_priority test with its validation. This matches the file-level summaries for threadmanager and the new test.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7674ee26-d91c-4247-976a-25c46174bd97
Commits

Reviewing files that changed from the base of the PR and between 88ab4ba and f32a60f.

Files selected for processing: 5

  • libs/core/runtime_local/tests/unit/CMakeLists.txt
  • libs/core/runtime_local/tests/unit/termination_detection_priority.cpp
  • libs/core/threadmanager/CMakeLists.txt
  • libs/core/threadmanager/include/hpx/threadmanager/threadmanager.hpp
  • libs/core/threadmanager/src/threadmanager.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant