Repository navigation
Fix semaphore publication with release ordering - #3697
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 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_addinbthread_sem_postfrom relaxed to release ordering, with a comment explaining why. - Add stress tests that check payload visibility for
bthread_sem_postandbthread_sem_post_nacross 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What problem does this PR solve?
Issue Number: resolve
Problem Summary:
bthread_sem_post()andbthread_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_releasein the shared post implementation, pairing with successful acquireCAS operations in
wait,timedwait, andtrywait.Side effects:
Performance effects:
Breaking backward compatibility:
Check List: