diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-01-09-09-36.gh-issue-152741.Fq7wZk.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-01-09-09-36.gh-issue-152741.Fq7wZk.rst new file mode 100644 index 00000000000000..c3f44b8895db88 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-01-09-09-36.gh-issue-152741.Fq7wZk.rst @@ -0,0 +1,4 @@ +Fix a data race on a thread state's thread id in the free-threaded build. +A starting thread now publishes its thread id with an atomic store so that +stop-the-world readers such as ``sys._current_exceptions()`` and +``sys._current_frames()`` no longer race with it. diff --git a/Python/pystate.c b/Python/pystate.c index e90642fa882db7..f65f7ac40fbc8e 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -175,9 +175,13 @@ bind_tstate(PyThreadState *tstate) // Currently we don't necessarily store the thread state // in thread-local storage (e.g. per-interpreter). - tstate->thread_id = PyThread_get_thread_ident(); + // Published while still attaching and already on the thread list, so a + // stop-the-world reader can load it concurrently; store atomically. + FT_ATOMIC_STORE_ULONG_RELAXED(tstate->thread_id, + PyThread_get_thread_ident()); #ifdef PY_HAVE_THREAD_NATIVE_ID - tstate->native_thread_id = PyThread_get_thread_native_id(); + FT_ATOMIC_STORE_ULONG_RELAXED(tstate->native_thread_id, + PyThread_get_thread_native_id()); #endif #ifdef Py_GIL_DISABLED @@ -2586,7 +2590,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) */ PyThreadState *tstate = NULL; _Py_FOR_EACH_TSTATE_BEGIN(interp, t) { - if (t->thread_id == id) { + if (FT_ATOMIC_LOAD_ULONG_RELAXED(t->thread_id) == id) { tstate = t; break; } @@ -2750,7 +2754,8 @@ _PyThread_CurrentFrames(void) if (frame == NULL) { continue; } - PyObject *id = PyLong_FromUnsignedLong(t->thread_id); + PyObject *id = PyLong_FromUnsignedLong( + FT_ATOMIC_LOAD_ULONG_RELAXED(t->thread_id)); if (id == NULL) { goto fail; } @@ -2814,7 +2819,8 @@ _PyThread_CurrentExceptions(void) if (err_info == NULL) { continue; } - PyObject *id = PyLong_FromUnsignedLong(t->thread_id); + PyObject *id = PyLong_FromUnsignedLong( + FT_ATOMIC_LOAD_ULONG_RELAXED(t->thread_id)); if (id == NULL) { goto fail; }