Skip to content

Fix semaphore publication with release ordering - #3697

Merged
wasphin merged 1 commit into
apache:masterfrom
chenBright:fix-sem
Nov 1, 2026
Merged

wasphin merged 1 commit into
apache:masterfrom
chenBright:fix-sem

Conversation

@chenBright

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

bthread_sem_post() and bthread_sem_post_n() used relaxed ordering when adding permits.
This did not publish preceding writes to consumers that successfully acquired permits without
blocking in butex_wait(), potentially causing data races on shared payloads.

What is changed and the side effects?

Changed:

Use memory_order_release in the shared post implementation, pairing with successful acquire
CAS operations in wait, timedwait, and trywait.

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

Copilot AI 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.

🟡 Changes recommended

The only remaining active finding is a nit in src/bthread/semaphore.cpp:117, where unrelated locals in bthread_sem_post() lost their const qualifiers, so the diff goes beyond the release-ordering fix it is meant to contain.

1 open issue
What changed in this PR

This PR fixes a memory-ordering bug in the bthread semaphore. bthread_sem_post() and bthread_sem_post_n() added permits with relaxed ordering. A waiter that gets a permit without blocking in butex_wait() could then miss writes made before the post, which can cause data races on shared data. The fix makes the permit update a release operation, which pairs with the successful acquire compare-and-swap (CAS) in wait, timedwait, and trywait.

Changes:

  • Change the permit fetch_add in bthread_sem_post from relaxed to release ordering, with a comment explaining why.
  • Add stress tests that check payload visibility for bthread_sem_post and bthread_sem_post_n across the wait, timed wait, and try-wait paths.
File Description
test/​bthread_semaphore_unittest.cpp Adds helper threads and tests that check payload visibility after a successful permit acquire for post and post_n.
src/​bthread/​semaphore.cpp Uses release ordering for the permit update in bthread_sem_post, which publishes earlier writes to waiters that acquire without blocking.

Review effort: Lite

Comment thread src/bthread/semaphore.cpp Resolved
@chenBright
chenBright requested review from wasphin and wwbmmm November 1, 2026 11:46

@wasphin wasphin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@wasphin
wasphin merged commit b009adb into apache:master Nov 1, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants