From c309c6651f9f72fdae8dc18cd65bcb1b637cdcbc Mon Sep 17 00:00:00 2001 From: Matt Page Date: Mon, 22 Apr 2024 15:30:22 -0700 Subject: [PATCH] Quiet TSAN warnings about remaining non-atomic accesses of `tstate->state` 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. --- Python/parking_lot.c | 3 ++- Python/pystate.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Python/parking_lot.c b/Python/parking_lot.c index d5877fef56e4d0..b368b500ccdfdb 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -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); } diff --git a/Python/pystate.c b/Python/pystate.c index 06806bd75fbcb2..bca28cebcc9059 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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);