[3.13] gh-137400: Fix thread-safety issues when profiling all threads (gh-137518) (gh-137733)

There were a few thread-safety issues when profiling or tracing all
threads via PyEval_SetProfileAllThreads or PyEval_SetTraceAllThreads:

* The loop over thread states could crash if a thread exits concurrently
  (in both the free threading and default build)
* The modification of `c_profilefunc` and `c_tracefunc` wasn't
  thread-safe on the free threading build.
(cherry picked from commit a10152f8fd)
This commit is contained in:
Sam Gross 2025-10-09 11:42:47 -04:00 committed by GitHub
parent f6186a3709
commit 16d2e97cf3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 29814 additions and 29714 deletions

View file

@ -1206,9 +1206,10 @@ sys__settraceallthreads(PyObject *module, PyObject *arg)
argument = arg;
}
PyEval_SetTraceAllThreads(func, argument);
PyInterpreterState *interp = _PyInterpreterState_GET();
if (_PyEval_SetTraceAllThreads(interp, func, argument) < 0) {
return NULL;
}
Py_RETURN_NONE;
}
@ -1286,8 +1287,10 @@ sys__setprofileallthreads(PyObject *module, PyObject *arg)
argument = arg;
}
PyEval_SetProfileAllThreads(func, argument);
PyInterpreterState *interp = _PyInterpreterState_GET();
if (_PyEval_SetProfileAllThreads(interp, func, argument) < 0) {
return NULL;
}
Py_RETURN_NONE;
}