Skip to content

Commit f29f878

Browse files
committed
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>
1 parent 481d5b5 commit f29f878

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Python/legacy_tracing.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,19 @@ setup_profile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg, PyObject
484484
}
485485
}
486486

487+
_PyEval_StopTheWorld(tstate->interp);
488+
487489
int delta = (func != NULL) - (tstate->c_profilefunc != NULL);
488490
tstate->c_profilefunc = func;
489491
*old_profileobj = tstate->c_profileobj;
490492
tstate->c_profileobj = Py_XNewRef(arg);
491493
tstate->interp->sys_profiling_threads += delta;
492494
assert(tstate->interp->sys_profiling_threads >= 0);
493-
return tstate->interp->sys_profiling_threads;
495+
Py_ssize_t ret = tstate->interp->sys_profiling_threads;
496+
497+
_PyEval_StartTheWorld(tstate->interp);
498+
499+
return ret;
494500
}
495501

496502
int

0 commit comments

Comments
 (0)