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.
"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;
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);