Skip to content

Fix late condition variable notifications after timeout - #8163

Merged
hkaiser merged 2 commits into
TheHPXProject:masterfrom
divyanshitalreja12:fix/condition-variable-late-wakeup-7916
Nov 22, 2026
Merged

hkaiser merged 2 commits into
TheHPXProject:masterfrom
divyanshitalreja12:fix/condition-variable-late-wakeup-7916

Conversation

@divyanshitalreja12

Copy link
Copy Markdown
Contributor

Fixes #7916.

Proposed changes

  • Wake a condition-variable waiter only if its tagged thread state still describes the suspension observed by the notifier.
  • Reuse the suspension-aware state transition introduced for timed wakes in Don't let a timer wake a thread after its suspension has ended #8108, so a notification racing a timeout cannot wake the waiter's next suspension.
  • Extend the late-wakeup regression to race timed waits against both notify_one and notify_all.

Validation

  • Built condition_variable_test and condition_variable_late_wakeup_test with AppleClang 21 in Release mode.
  • Both focused CTest cases passed; the late-wakeup case passed 10 repeated runs and a run with --hpx:queuing=local-workrequesting-fifo.
  • clang-format --dry-run --Werror and git diff --check passed.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

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: 5a3afaaf-7602-409b-8e5d-824098372c25
Commits

Reviewing files that changed from the base of the PR and between 89219dd and f92678e.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of timed waits when a notification arrives near the deadline. A wait now reports either a notification or a timeout, depending on which occurs first. Notifications that arrive after a waiter has left its wait no longer cause an unintended wakeup during a subsequent suspension.
  • Tests
    • Added repeated checks for notification races near timed-wait deadlines and for unintended wakeups during a subsequent suspension.

The condition-variable notifier now transitions the specific observed suspended thread state to pending with a signaled restart state. The unit test adds timed notification races for notify_one and notify_all and checks for unexpected wakeups during a later suspension.

Changes: Condition variable late-wakeup handling

Layer / File(s) Summary
Resume the observed suspension
libs/core/synchronization/src/detail/condition_variable.cpp
resume_waiter records the observed suspended thread state and uses it to transition that suspension to pending with a signaled restart state. It returns if the waiter has left its wait or no suspended state was captured.
Test timed notification races
libs/core/synchronization/tests/unit/condition_variable_late_wakeup.cpp
The test races timed waits with notify_one and notify_all using a 2 ms deadline. It accepts signaled or timeout, then checks for unexpected wakeups during a subsequent suspension. Both timed variants run 100 times.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Suggested labels: type: defect

Merge Risk: Minimal · previous review 89219

This change stops a late condition-variable notification from waking a waiter's later, unrelated suspension after a timeout. No concrete defect was found in the new notifier logic, and the extended race test covers both notify_one and notify_all. The merge risk is minimal.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #7916 has two coding requirements. (1) Notification side: resume a waiter only from the exact suspension observed, and never hand the wakeup to another thread. The diff in condition_variable.cpp… Show that the timer wakeup for a timed suspension uses the same exact-suspension check at the reviewed base or head. The check must cover the case where a notification wakes the waiter and the waiter suspends again before its timer is cance…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title "Fix late condition variable notifications after timeout" matches the main change: the PR stops a notification from waking a condition-variable waiter's later suspension when a timeout races…
Description check Passed The description states the fix (wake a waiter only if its tagged thread state still matches the observed suspension), reuses the suspension-aware transition from #8108, and extends the late-wakeup reg…
Out of Scope Changes check Passed All changed code lies within the scope of #7916. The condition_variable.cpp change replaces the suspended-flag logic in resume_waiter with suspended-state capture and set_suspended_thread_state. The t…
Full details: Title check

Explanation

The title "Fix late condition variable notifications after timeout" matches the main change: the PR stops a notification from waking a condition-variable waiter's later suspension when a timeout races the notification. The raw summary shows the resume_waiter change in condition_variable.cpp and a late-wakeup regression test, both consistent with the title.

Full details: Description check

Explanation

The description states the fix (wake a waiter only if its tagged thread state still matches the observed suspension), reuses the suspension-aware transition from #8108, and extends the late-wakeup regression test to notify_one and notify_all. It also lists build and test validation. This matches the changeset described in the raw summary.

Full details: Out of Scope Changes check

Explanation

All changed code lies within the scope of #7916. The condition_variable.cpp change replaces the suspended-flag logic in resume_waiter with suspended-state capture and set_suspended_thread_state. The test change adds test_timed_round, which covers the timed-wait notification race described in the issue. The copyright lines added to two files are administrative and do not change behavior. No unrelated behavior change appears in the reviewed diff.

Full details: Linked Issues check

Explanation

Issue #7916 has two coding requirements. (1) Notification side: resume a waiter only from the exact suspension observed, and never hand the wakeup to another thread. The diff in condition_variable.cpp addresses this. resume_waiter now stores the observed suspended thread_state, including its tag. It then calls threads::detail::set_suspended_thread_state(id, tag, pending, signaled, priority) and returns. It no longer calls ctx.resume(), so the set_thread_state/reschedule_active path is not used for this case. The reviewed diff does not show the internal failure behavior of set_suspended_thread_state. The requirement is met as far as the diff shows. (2) Timer side: the issue states that a timed suspension's timer can wake a later suspension after a notification has already woken the waiter. The PR description says the existing transition from #8108 covers timed wakes. The PR diff does not change the timer path, and the PR objectives state that it does not validate the timer-side race. The base-side implementation of the timer path was not inspected, so the timer-side requirement cannot be confirmed as met. The added test test_timed_round races notify_one and notify_all against a timed wait and counts stray wakeups. Its comment states that it is not a deterministic reproducer of #7916, so it gives only probabilistic coverage.

Resolution

Show that the timer wakeup for a timed suspension uses the same exact-suspension check at the reviewed base or head. The check must cover the case where a notification wakes the waiter and the waiter suspends again before its timer is cancelled. Name the file and function that implement this. Alternatively, add a change or a test that covers this timer-side case.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • 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: f2787ebe-ae51-42d4-a8b7-bbe32ae61756
Commits

Reviewing files that changed from the base of the PR and between 610fb0f and 89219dd.

Files selected for processing: 1

  • libs/core/synchronization/tests/unit/condition_variable_late_wakeup.cpp
Files skipped from review as they are similar to previous changes: 1

  • libs/core/synchronization/tests/unit/condition_variable_late_wakeup.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 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.

@hkaiser hkaiser added this to the 2.1.0 milestone Nov 21, 2026
@divyanshitalreja12
divyanshitalreja12 force-pushed the fix/condition-variable-late-wakeup-7916 branch from 1d808f7 to 610fb0f Compare November 21, 2026 19:56

@hkaiser hkaiser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is the fix #7916 asked for: the notifier now changes the waiter's state only from the exact suspension it observed (state and tag, through set_suspended_thread_state from #8108), and never hands the wakeup to another thread, so a timeout that ends the suspension in between turns the notification into a no-op.

One question: does the new test_timed_round fail on master? The window needs the timer to fire between reading the state and the wakeup, and the reproducer in #7916 only hit it with a sleep injected into resume_waiter. If it doesn't, that's fine, but please say in the test's comment that it exercises the path rather than reproducing the race. LGTM otherwise.

@divyanshitalreja12

Copy link
Copy Markdown
Contributor Author

The current cross-platform build failures are in serialization code from master, before the condition-variable test builds. Master changed extra_data_id_type to std::size_t, while post_receive_actions::id() and preprocess_keep_alive::id() still return addresses; Linux, Windows, and CodeQL all report the same pointer-to-integer conversion error. This PR does not change those files.

I opened #8188 with the two-line ID fix as a separate PR. Its affected serialization target builds and the focused test passes locally. Once #8188 lands, this PR's CI can be rerun against the corrected master. Separately, as requested in the review, I verified that test_timed_round passed ten runs against the unfixed condition-variable implementation and documented in the test that it exercises the path without deterministically reproducing #7916.

@hkaiser hkaiser closed this Nov 21, 2026
@hkaiser hkaiser reopened this Nov 21, 2026
@hkaiser hkaiser closed this Nov 22, 2026
@hkaiser hkaiser reopened this Nov 22, 2026
@hkaiser hkaiser closed this Nov 22, 2026
@hkaiser hkaiser reopened this Nov 22, 2026
Signed-off-by: Divyanshi Talreja <divyanshitalreja12@gmail.com>
Signed-off-by: Divyanshi Talreja <divyanshitalreja12@gmail.com>
@divyanshitalreja12
divyanshitalreja12 force-pushed the fix/condition-variable-late-wakeup-7916 branch from 89219dd to f92678e Compare November 22, 2026 09:38

@hkaiser hkaiser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

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.

condition_variable: a timed wait can still receive a late wakeup

2 participants