Skip to content

Commit 718fc5b

Browse files
[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 f183996) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
1 parent 893707c commit 718fc5b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Python/pystate.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,10 @@ PyThreadState_Clear(PyThreadState *tstate)
17931793
"PyThreadState_Clear: warning: thread still has a generator\n");
17941794
}
17951795

1796+
#ifdef Py_GIL_DISABLED
1797+
PyMutex_Lock(&_PyRuntime.ceval.sys_trace_profile_mutex);
1798+
#endif
1799+
17961800
if (tstate->c_profilefunc != NULL) {
17971801
tstate->interp->sys_profiling_threads--;
17981802
tstate->c_profilefunc = NULL;
@@ -1801,6 +1805,11 @@ PyThreadState_Clear(PyThreadState *tstate)
18011805
tstate->interp->sys_tracing_threads--;
18021806
tstate->c_tracefunc = NULL;
18031807
}
1808+
1809+
#ifdef Py_GIL_DISABLED
1810+
PyMutex_Unlock(&_PyRuntime.ceval.sys_trace_profile_mutex);
1811+
#endif
1812+
18041813
Py_CLEAR(tstate->c_profileobj);
18051814
Py_CLEAR(tstate->c_traceobj);
18061815

0 commit comments

Comments
 (0)