From e8225ab6be13c8b229c52917badd29eefa3c681f Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 22 Jul 2025 01:37:25 +0530 Subject: [PATCH] fix race in PyThreadState_Clear on sys_tracing_threads --- Python/pystate.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Python/pystate.c b/Python/pystate.c index 0d4c26f92cec90..04ca6edb4aaa0e 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1682,6 +1682,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; @@ -1690,6 +1694,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);