[3.13] gh-154937: Fix `_thread._shutdown()` racing with `_thread.start_joinable_thread` on `ThreadHandle.ident` (GH-155085) (#155125)

Yhg1s <[email protected]> Tue, 04 Aug 2026 05:28:02 -0400 (EDT)
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/75344c0683eb2db6594fd5126b80a1c314b96d3c
commit: 75344c0683eb2db6594fd5126b80a1c314b96d3c
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: Yhg1s <[email protected]>
date: 2026-08-04T09:27:41Z
summary:

[3.13] gh-154937: Fix `_thread._shutdown()` racing with `_thread.start_joinable_thread` on `ThreadHandle.ident` (GH-155085) (#155125)

* gh-154937: Fix `_thread._shutdown()` racing with `_thread.start_joinable_thread` on `ThreadHandle.ident` (GH-155085)
(cherry picked from commit a4095ee272f5e5082281a94a0856dde3c2f323f9)

Co-authored-by: Edward Xu <[email protected]>

files:
A Misc/NEWS.d/next/Core_and_Builtins/2026-08-02-17-55-29.gh-issue-154937.m9IBba.rst
M Modules/_threadmodule.c

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-08-02-17-55-29.gh-issue-154937.m9IBba.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-02-17-55-29.gh-issue-154937.m9IBba.rst
new file mode 100644
index 000000000000000..e307b95216934aa
--- /dev/null
+++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-08-02-17-55-29.gh-issue-154937.m9IBba.rst
@@ -0,0 +1,3 @@
+Fix a data race on thread handle identifiers when ``_thread._shutdown()``
+runs concurrently with the startup of non-daemon threads in the
+free-threaded build.
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index bc9433afe41ef10..4ce32a8fa2fe0bc 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -2349,7 +2349,7 @@ thread_shutdown(PyObject *self, PyObject *args)
         struct llist_node *node;
         llist_for_each_safe(node, &state->shutdown_handles) {
             ThreadHandle *cur = llist_data(node, ThreadHandle, shutdown_node);
-            if (cur->ident != ident) {
+            if (ThreadHandle_ident(cur) != ident) {
                 ThreadHandle_incref(cur);
                 handle = cur;
                 break;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]