[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

@ -1012,6 +1012,8 @@ set_version_raw(uintptr_t *ptr, uint32_t version)
static void
set_global_version(PyThreadState *tstate, uint32_t version)
{
ASSERT_WORLD_STOPPED();
assert((version & _PY_EVAL_EVENTS_MASK) == 0);
PyInterpreterState *interp = tstate->interp;
set_version_raw(&interp->ceval.instrumentation_version, version);
@ -1908,28 +1910,27 @@ _Py_Instrument(PyCodeObject *code, PyInterpreterState *interp)
static int
instrument_all_executing_code_objects(PyInterpreterState *interp) {
instrument_all_executing_code_objects(PyInterpreterState *interp)
{
ASSERT_WORLD_STOPPED();
_PyRuntimeState *runtime = &_PyRuntime;
HEAD_LOCK(runtime);
PyThreadState* ts = PyInterpreterState_ThreadHead(interp);
HEAD_UNLOCK(runtime);
while (ts) {
int err = 0;
HEAD_LOCK(&_PyRuntime);
for (PyThreadState *ts = interp->threads.head; ts != NULL; ts = ts->next) {
_PyInterpreterFrame *frame = ts->current_frame;
while (frame) {
if (frame->owner != FRAME_OWNED_BY_CSTACK) {
if (instrument_lock_held(_PyFrame_GetCode(frame), interp)) {
return -1;
err = instrument_lock_held(_PyFrame_GetCode(frame), interp);
if (err) {
goto done;
}
}
frame = frame->previous;
}
HEAD_LOCK(runtime);
ts = PyThreadState_Next(ts);
HEAD_UNLOCK(runtime);
}
return 0;
done:
HEAD_UNLOCK(&_PyRuntime);
return err;
}
static void
@ -1975,6 +1976,7 @@ check_tool(PyInterpreterState *interp, int tool_id)
int
_PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events)
{
ASSERT_WORLD_STOPPED();
assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS);
PyThreadState *tstate = _PyThreadState_GET();
PyInterpreterState *interp = tstate->interp;
@ -1983,33 +1985,28 @@ _PyMonitoring_SetEvents(int tool_id, _PyMonitoringEventSet events)
return -1;
}
int res;
_PyEval_StopTheWorld(interp);
uint32_t existing_events = get_events(&interp->monitors, tool_id);
if (existing_events == events) {
res = 0;
goto done;
return 0;
}
set_events(&interp->monitors, tool_id, events);
uint32_t new_version = global_version(interp) + MONITORING_VERSION_INCREMENT;
if (new_version == 0) {
PyErr_Format(PyExc_OverflowError, "events set too many times");
res = -1;
goto done;
return -1;
}
set_global_version(tstate, new_version);
#ifdef _Py_TIER2
_Py_Executors_InvalidateAll(interp, 1);
#endif
res = instrument_all_executing_code_objects(interp);
done:
_PyEval_StartTheWorld(interp);
return res;
return instrument_all_executing_code_objects(interp);
}
int
_PyMonitoring_SetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringEventSet events)
{
ASSERT_WORLD_STOPPED();
assert(0 <= tool_id && tool_id < PY_MONITORING_TOOL_IDS);
PyInterpreterState *interp = _PyInterpreterState_GET();
assert(events < (1 << _PY_MONITORING_LOCAL_EVENTS));
@ -2021,26 +2018,18 @@ _PyMonitoring_SetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringEvent
return -1;
}
int res;
_PyEval_StopTheWorld(interp);
if (allocate_instrumentation_data(code)) {
res = -1;
goto done;
return -1;
}
_Py_LocalMonitors *local = &code->_co_monitoring->local_monitors;
uint32_t existing_events = get_local_events(local, tool_id);
if (existing_events == events) {
res = 0;
goto done;
return 0;
}
set_local_events(local, tool_id, events);
res = force_instrument_lock_held(code, interp);
done:
_PyEval_StartTheWorld(interp);
return res;
return force_instrument_lock_held(code, interp);
}
int
@ -2238,7 +2227,11 @@ monitoring_set_events_impl(PyObject *module, int tool_id, int event_set)
return NULL;
}
event_set &= ~C_RETURN_EVENTS;
if (_PyMonitoring_SetEvents(tool_id, event_set)) {
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
int err = _PyMonitoring_SetEvents(tool_id, event_set);
_PyEval_StartTheWorld(interp);
if (err) {
return NULL;
}
Py_RETURN_NONE;
@ -2315,7 +2308,11 @@ monitoring_set_local_events_impl(PyObject *module, int tool_id,
return NULL;
}
if (_PyMonitoring_SetLocalEvents((PyCodeObject*)code, tool_id, event_set)) {
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyEval_StopTheWorld(interp);
int err = _PyMonitoring_SetLocalEvents((PyCodeObject*)code, tool_id, event_set);
_PyEval_StartTheWorld(interp);
if (err) {
return NULL;
}
Py_RETURN_NONE;