Skip to content

Acquire the last worker's departure before checking that the pool stops - #8214

Merged
hkaiser merged 1 commit into
masterfrom
fix-outside-creation-acq-rel
Nov 24, 2026
Merged

hkaiser merged 1 commit into
masterfrom
fix-outside-creation-acq-rel

Conversation

@hkaiser

@hkaiser hkaiser commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A creation of a thread from outside the workers of a pool (#8118) increments the counter of the workers in the scheduling loop and then checks whether the pool has reached the stopping state. The increment was relaxed: if it read the count the last worker left, the check could still see an earlier state, and the thread would be created with no worker left to run it. The increment is an acquire now (acq_rel): each worker saw its state stopping before its decrement, which is a release, and the decrements form one release sequence, so the check sees the states stopping as well.

This was raised in the review of #8118 and again on the V2.0.1 release PR #8099. x86 doesn't reorder here (a read-modify-write is a full barrier), ARM can.

begin_outside_creation incremented the count of the workers in the scheduling loop with a relaxed operation and then checked whether the pool reached the stopping state. If the increment read the count the last worker left, the check could still see an earlier state, and the thread was created with no worker left to run it. The increment is acq_rel now: each worker saw its state stopping before its decrement (a release), and the decrements form one release sequence, so the check sees the states stopping as well.

Signed-off-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>
@hkaiser hkaiser added this to the 2.1.0 milestone Nov 24, 2026
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved coordination between worker activity and scheduler shutdown to support more reliable thread management.

The begin_outside_creation counter increment now uses acquire-release ordering. Updated comments describe synchronization with worker decrements and the stopping-state check.

Changes: Outside-creation synchronization

Layer / File(s) Summary
Counter increment ordering
libs/core/threading_base/src/scheduler_base.cpp
begin_outside_creation increments workers_in_loop_ with acquire-release ordering. Comments describe synchronization with worker decrements and the stopping-state check.

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

Change: Bug fix

Possibly related PRs

  • TheHPXProject/hpx#8118: Added begin_outside_creation and end_outside_creation; this PR changes the increment’s memory ordering.

Suggested labels: type: race condition, type: defect, category: threadmanager

Suggested reviewers: shivansh023023, hungtruongowolf

Merge Risk: Minimal · up to b20fc

This change makes the thread-creation path that runs outside the pool's workers synchronize correctly with workers leaving during shutdown, which matters on weakly ordered CPUs such as ARM. It only adds ordering, so the chance of regression is very low. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Title check Inconclusive The title names the memory-ordering change to the last worker's departure before the stopping-state check, but the changeset only changes the increment in begin_outside_creation from relaxed to acq_re… Reword the title to name the actual change, for example: 'Use acq_rel ordering for worker count increment in begin_outside_creation'. This states the modified operation and its location.
Passed Passed checks: 3 passed

Check name Status Explanation
Description check Passed The description explains the change. It says a thread creation from outside the pool increments the worker count with a relaxed ordering, which can let the thread be created with no worker left. It sa…
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: Description check

Explanation

The description explains the change. It says a thread creation from outside the pool increments the worker count with a relaxed ordering, which can let the thread be created with no worker left. It says the increment is now acq_rel, and it cites the related PRs and the x86 and ARM behavior. It matches the changeset, which changes the increment in begin_outside_creation.

Full details: Title check

Explanation

The title names the memory-ordering change to the last worker's departure before the stopping-state check, but the changeset only changes the increment in begin_outside_creation from relaxed to acq_rel. The title says 'Acquire the last worker's departure', which mischaracterizes the change, since the change strengthens the increment, not the departure. It is related to the change but its wording is imprecise. The title is specific and not vague.

Resolution

Reword the title to name the actual change, for example: 'Use acq_rel ordering for worker count increment in begin_outside_creation'. This states the modified operation and its location.

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: 245de148-104d-490e-a8ca-8aeeba6d1358
Commits

Reviewing files that changed from the base of the PR and between c571037 and b20fcda.

Files selected for processing: 1

  • libs/core/threading_base/src/scheduler_base.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 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 merged commit 39fa184 into master Nov 24, 2026
161 checks passed
@hkaiser
hkaiser deleted the fix-outside-creation-acq-rel branch November 24, 2026 17:57
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