Skip to content

[3.14] gh-137400: Fix a crash when disabling profiling across all threads (gh-137471) #137648

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

Open
wants to merge 1 commit into
base: 3.14
Choose a base branch
from
Open
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
gh-137400: Fix a crash when disabling profiling across all threads (g…
…h-137471)

The `PyEval_SetProfileAllThreads` function and other related functions
had a race condition on `tstate->c_profilefunc` that could lead to a
crash when disable profiling or tracing on all threads while another
thread is starting to profile or trace a a call.

There are still potential crashes when threads exit concurrently with
profiling or tracing be enabled/disabled across all threads.
(cherry picked from commit 3626928)

Co-authored-by: Sam Gross <colesbury@gmail.com>
  • Loading branch information
colesbury authored and miss-islington committed Aug 11, 2025
commit 77f9ca63e3404d7a86d88defb045154ca14f28ee
34 changes: 34 additions & 0 deletions Lib/test/test_free_threading/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,40 @@ def during_threads(self):
self.set = not self.set


@threading_helper.requires_working_threading()
class SetProfileAllMultiThreaded(TestCase):
def test_profile_all_threads(self):
done = threading.Event()

def func():
pass

def bg_thread():
while not done.is_set():
func()
func()
func()
func()
func()

def my_profile(frame, event, arg):
return None

bg_threads = []
for i in range(10):
t = threading.Thread(target=bg_thread)
t.start()
bg_threads.append(t)

for i in range(100):
threading.setprofile_all_threads(my_profile)
threading.setprofile_all_threads(None)

done.set()
for t in bg_threads:
t.join()


class TraceBuf:
def __init__(self):
self.traces = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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: 8 additions & 2 deletions Python/legacy_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,16 @@ setup_profile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject
}
}

_PyEval_StopTheWorld(tstate->interp);
int delta = (func != NULL) - (tstate->c_profilefunc != NULL);
tstate->c_profilefunc = func;
*old_profileobj = tstate->c_profileobj;
tstate->c_profileobj = Py_XNewRef(arg);
tstate->interp->sys_profiling_threads += delta;
assert(tstate->interp->sys_profiling_threads >= 0);
return tstate->interp->sys_profiling_threads;
Py_ssize_t profiling_threads = tstate->interp->sys_profiling_threads;
_PyEval_StartTheWorld(tstate->interp);
return profiling_threads;
}

int
Expand Down Expand Up @@ -581,13 +584,16 @@ setup_tracing(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject
}
}

_PyEval_StopTheWorld(tstate->interp);
int delta = (func != NULL) - (tstate->c_tracefunc != NULL);
tstate->c_tracefunc = func;
*old_traceobj = tstate->c_traceobj;
tstate->c_traceobj = Py_XNewRef(arg);
tstate->interp->sys_tracing_threads += delta;
assert(tstate->interp->sys_tracing_threads >= 0);
return tstate->interp->sys_tracing_threads;
Py_ssize_t tracing_threads = tstate->interp->sys_tracing_threads;
_PyEval_StartTheWorld(tstate->interp);
return tracing_threads;
}

int
Expand Down
Loading