https://github.com/python/cpython/commit/7aeaa5af2c32f1f87b59f36f89e19284e24b26b9
commit: 7aeaa5af2c32f1f87b59f36f89e19284e24b26b9
branch: main
author: Sam Gross <colesb...@gmail.com>
committer: colesbury <colesb...@gmail.com>
date: 2025-02-27T13:57:19-05:00
summary:

gh-130091: Reorder `_PyThreadState_Attach` to avoid data race (gh-130092)

This moves `tstate_activate()` down to avoid a data race in the free
threading build on the `_PyRuntime`'s thread-local `autoTSSkey`. This
key is deleted during runtime finalization, which may happen
concurrently with a call to `_PyThreadState_Attach`.

The earlier `tstate_try/wait_attach` ensures that the thread is blocked
before it attempts to access the deleted `autoTSSkey`.

This fixes a TSAN reported data race in
`test_threading.test_import_from_another_thread`.

files:
M Python/pystate.c

diff --git a/Python/pystate.c b/Python/pystate.c
index 9ebd9fdea60746..72e2627e95b559 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -2099,11 +2099,10 @@ _PyThreadState_Attach(PyThreadState *tstate)
 
         // XXX assert(tstate_is_alive(tstate));
         current_fast_set(&_PyRuntime, tstate);
-        tstate_activate(tstate);
-
         if (!tstate_try_attach(tstate)) {
             tstate_wait_attach(tstate);
         }
+        tstate_activate(tstate);
 
 #ifdef Py_GIL_DISABLED
         if (_PyEval_IsGILEnabled(tstate) && !tstate->_status.holds_gil) {

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to