Skip to content
Closed
Changes from 1 commit
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
Next Next commit
gh-137400: Stop the world when swapping profile functions
One thread's profile function or profile object can be changed from
another thread, for instance by `sys._setprofileallthreads`. This can
lead to crashes unless the two threads are synchronized. We need
a stop-the-world to ensure that the thread whose profile function or
profile object is being changed is in a state where it's not in the
middle of using either of those pointers.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
  • Loading branch information
godlygeek committed Aug 6, 2025
commit f29f87872941ac061ef64034373730683e0203fb
8 changes: 7 additions & 1 deletion Python/legacy_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,19 @@ 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 ret = tstate->interp->sys_profiling_threads;

_PyEval_StartTheWorld(tstate->interp);

return ret;
}

int
Expand Down
Loading