mirror of
https://github.com/python/cpython.git
synced 2025-10-29 04:35:05 +00:00
gh-137400: Fix thread-safety issues when profiling all threads (gh-137518)
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.
This commit is contained in:
parent
923d68655b
commit
a10152f8fd
11 changed files with 431 additions and 241 deletions
|
|
@ -2510,21 +2510,10 @@ PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
|
|||
void
|
||||
PyEval_SetProfileAllThreads(Py_tracefunc func, PyObject *arg)
|
||||
{
|
||||
PyThreadState *this_tstate = _PyThreadState_GET();
|
||||
PyInterpreterState* interp = this_tstate->interp;
|
||||
|
||||
_PyRuntimeState *runtime = &_PyRuntime;
|
||||
HEAD_LOCK(runtime);
|
||||
PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
|
||||
HEAD_UNLOCK(runtime);
|
||||
|
||||
while (ts) {
|
||||
if (_PyEval_SetProfile(ts, func, arg) < 0) {
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetProfileAllThreads");
|
||||
}
|
||||
HEAD_LOCK(runtime);
|
||||
ts = PyThreadState_Next(ts);
|
||||
HEAD_UNLOCK(runtime);
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
if (_PyEval_SetProfileAllThreads(interp, func, arg) < 0) {
|
||||
/* Log _PySys_Audit() error */
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetProfileAllThreads");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2541,21 +2530,10 @@ PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
|
|||
void
|
||||
PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *arg)
|
||||
{
|
||||
PyThreadState *this_tstate = _PyThreadState_GET();
|
||||
PyInterpreterState* interp = this_tstate->interp;
|
||||
|
||||
_PyRuntimeState *runtime = &_PyRuntime;
|
||||
HEAD_LOCK(runtime);
|
||||
PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
|
||||
HEAD_UNLOCK(runtime);
|
||||
|
||||
while (ts) {
|
||||
if (_PyEval_SetTrace(ts, func, arg) < 0) {
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetTraceAllThreads");
|
||||
}
|
||||
HEAD_LOCK(runtime);
|
||||
ts = PyThreadState_Next(ts);
|
||||
HEAD_UNLOCK(runtime);
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
if (_PyEval_SetTraceAllThreads(interp, func, arg) < 0) {
|
||||
/* Log _PySys_Audit() error */
|
||||
PyErr_FormatUnraisable("Exception ignored in PyEval_SetTraceAllThreads");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue