From eb46a1865c11564a222a0331958d78e487cccff0 Mon Sep 17 00:00:00 2001 From: Matt Page Date: Fri, 12 Apr 2024 13:23:26 -0700 Subject: [PATCH 1/2] Quiet TSAN warning about a data race between `start_the_world()` and `tstate_try_attach()` TSAN erroneously reports a data race between the `_Py_atomic_compare_exchange_int` on `tstate->state` in `tstate_try_attach()` and the non-atomic load of `tstate->state` in `start_the_world`. The `_Py_atomic_compare_exchange_int` fails, but TSAN erroneously treats it as a store. --- Python/pystate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index 4a52f6444ba10a..57aceda0a6c1da 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2190,7 +2190,7 @@ start_the_world(struct _stoptheworld_state *stw) PyThreadState *t; _Py_FOR_EACH_THREAD(stw, i, t) { if (t != stw->requester) { - assert(t->state == _Py_THREAD_SUSPENDED); + assert(_Py_atomic_load_int(&t->state) == _Py_THREAD_SUSPENDED); _Py_atomic_store_int(&t->state, _Py_THREAD_DETACHED); _PyParkingLot_UnparkAll(&t->state); } From 61bd2fa6dff8641ce3ec2c2a01fc7fe5014ffbbc Mon Sep 17 00:00:00 2001 From: Matt Page Date: Fri, 12 Apr 2024 14:44:52 -0700 Subject: [PATCH 2/2] Use a relaxed load --- Python/pystate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index 57aceda0a6c1da..4c514d4650ceac 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2190,7 +2190,8 @@ start_the_world(struct _stoptheworld_state *stw) PyThreadState *t; _Py_FOR_EACH_THREAD(stw, i, t) { if (t != stw->requester) { - assert(_Py_atomic_load_int(&t->state) == _Py_THREAD_SUSPENDED); + assert(_Py_atomic_load_int_relaxed(&t->state) == + _Py_THREAD_SUSPENDED); _Py_atomic_store_int(&t->state, _Py_THREAD_DETACHED); _PyParkingLot_UnparkAll(&t->state); }