Skip to content

Commit

Permalink
Quiet TSAN warnings about remaining non-atomic accesses of `tstate->s…
Browse files Browse the repository at this point in the history
…tate`

These should be the last instances of TSAN warning of data races when accessing
`tstate->state` non-atomically.

TSAN reports a race between accessing `tstate->state` in `_PyThreadState_Suspend()`
and the compare/exhange in `park_detached_threads`. This is a false positive due
to TSAN modeling failed compare/exchange operations as writes.

TSAN reports a race between accessing `tstate->state` in `_PySemaphore_Wait()`
and the compare/exchange in `park_detached_threads`. This is the same issue that
we saw in gh-117830.
  • Loading branch information
mpage committed Apr 22, 2024
1 parent fc21c7f commit c309c66
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Python/parking_lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ _PySemaphore_Wait(_PySemaphore *sema, PyTime_t timeout, int detach)
PyThreadState *tstate = NULL;
if (detach) {
tstate = _PyThreadState_GET();
if (tstate && tstate->state == _Py_THREAD_ATTACHED) {
if (tstate && _Py_atomic_load_int_relaxed(&tstate->state) ==
_Py_THREAD_ATTACHED) {
// Only detach if we are attached
PyEval_ReleaseThread(tstate);
}
Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,7 @@ _PyThreadState_Suspend(PyThreadState *tstate)
{
_PyRuntimeState *runtime = &_PyRuntime;

assert(tstate->state == _Py_THREAD_ATTACHED);
assert(_Py_atomic_load_int_relaxed(&tstate->state) == _Py_THREAD_ATTACHED);

struct _stoptheworld_state *stw = NULL;
HEAD_LOCK(runtime);
Expand Down

0 comments on commit c309c66

Please sign in to comment.