Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: 3.13
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ struct _ceval_runtime_state;

// Export for '_lsprof' shared extension
PyAPI_FUNC(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
extern int _PyEval_SetProfileAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg);

extern int _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg);
extern int _PyEval_SetTraceAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg);

extern int _PyEval_SetOpcodeTrace(PyFrameObject *f, bool enable);

Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ struct _is {
PyDict_WatchCallback builtins_dict_watcher;

_Py_GlobalMonitors monitors;
bool sys_profile_initialized;
bool sys_trace_initialized;
_PyOnceFlag sys_profile_once_flag;
_PyOnceFlag sys_trace_once_flag;
Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][_PY_MONITORING_EVENTS];
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_free_threading/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ def during_threads(self):
self.set = not self.set


@threading_helper.requires_working_threading()
class SetProfileAllThreadsMultiThreaded(InstrumentationMultiThreadedMixin, TestCase):
"""Uses threading.setprofile_all_threads and repeatedly toggles instrumentation on and off"""

def setUp(self):
self.set = False
self.called = False

def after_test(self):
self.assertTrue(self.called)

def tearDown(self):
threading.setprofile_all_threads(None)

def trace_func(self, frame, event, arg):
self.called = True
return self.trace_func

def during_threads(self):
if self.set:
threading.setprofile_all_threads(self.trace_func)
else:
threading.setprofile_all_threads(None)
self.set = not self.set


@threading_helper.requires_working_threading()
class SetProfileAllMultiThreaded(TestCase):
def test_profile_all_threads(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a crash in the :term:`free threading` build when disabling profiling or
tracing across all threads with :c:func:`PyEval_SetProfileAllThreads` or
:c:func:`PyEval_SetTraceAllThreads` or their Python equivalents
:func:`threading.settrace_all_threads` and
:func:`threading.setprofile_all_threads`.
10 changes: 9 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ dummy_func(

tier1 inst(RESUME, (--)) {
assert(frame == tstate->current_frame);
if (tstate->tracing == 0) {
#ifdef Py_GIL_DISABLED
// For thread-safety, we need to check instrumentation version
// even when tracing. Otherwise, another thread may concurrently
// re-write the bytecode while we are executing this function.
int check_instrumentation = 1;
#else
int check_instrumentation = (tstate->tracing == 0);
#endif
if (check_instrumentation) {
uintptr_t global_version =
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
~_PY_EVAL_EVENTS_MASK;
Expand Down
38 changes: 8 additions & 30 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2334,21 +2334,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");
}
}

Expand All @@ -2365,21 +2354,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");
}
}

Expand Down
10 changes: 9 additions & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 32 additions & 35 deletions Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading