gh-117657: Quiet more TSAN warnings due to incorrect modeling of compare/exchange #117830
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.
TSAN reports (example) data races between the non-atomic loads of
tstate->state
indetach_thread()
andtstate_set_detached()
:cpython/Python/pystate.c
Lines 2067 to 2080 in eca5362
cpython/Python/pystate.c
Lines 2003 to 2008 in eca5362
and the atomic compare/exchange of
tstate->state
inpark_detached_threads()
:cpython/Python/pystate.c
Lines 2159 to 2170 in eca5362
I believe this is due to a bug with how TSAN models compare/exchange. Note that this appears to be a different issue than what we're seeing in #117828; the compare/exhange is succeeding in this case.
According to the standard,
However, according to the standard, A strongly happens-before B if any of the following are true:
Using this we can establish a happens-before relationship between each non-atomic load and the compare/exchange:
tstate->state
that happens intstate_set_detached()
. Therefore, each non-atomic load happens-before the seq-cst store.tstate->state
intstate_set_detached()
synchronizes-with the compare/exchange totstate->state
inpark_detached_threads()
. Thus the seq-cst store happens-before the compare/exchange.tstate->state
, therefore they should not be flagged as a data races.