https://github.com/python/cpython/commit/718fc5b1393d9ce52fc0353fb361818e865e3e45
commit: 718fc5b1393d9ce52fc0353fb361818e865e3e45
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-07-21T21:01:14Z
summary:

[3.14] gh-136870: fix data race in `PyThreadState_Clear` on 
`sys_tracing_threads` (GH-136951) (#136953)

gh-136870: fix data race in `PyThreadState_Clear` on `sys_tracing_threads` 
(GH-136951)

In free-threading, multiple threads can be cleared concurrently as such the 
modifications on `sys_tracing_threads` should be done while holding the profile 
lock, otherwise it can race with other threads setting up profiling.
(cherry picked from commit f183996eb77fd2d5662c62667298c292c943ebf5)

Co-authored-by: Kumar Aditya <[email protected]>

files:
M Python/pystate.c

diff --git a/Python/pystate.c b/Python/pystate.c
index 015e9f8725c1af..aa2c4bd51814c3 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -1793,6 +1793,10 @@ PyThreadState_Clear(PyThreadState *tstate)
           "PyThreadState_Clear: warning: thread still has a generator\n");
     }
 
+#ifdef Py_GIL_DISABLED
+    PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex);
+#endif
+
     if (tstate->c_profilefunc != NULL) {
         tstate->interp->sys_profiling_threads--;
         tstate->c_profilefunc = NULL;
@@ -1801,6 +1805,11 @@ PyThreadState_Clear(PyThreadState *tstate)
         tstate->interp->sys_tracing_threads--;
         tstate->c_tracefunc = NULL;
     }
+
+#ifdef Py_GIL_DISABLED
+    PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex);
+#endif
+
     Py_CLEAR(tstate->c_profileobj);
     Py_CLEAR(tstate->c_traceobj);
 

_______________________________________________
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]

Reply via email to