From 6a249584eb6f778bf76adf7179a1e1f53c193d3e Mon Sep 17 00:00:00 2001 From: Matt Page Date: Mon, 18 Nov 2024 22:08:31 -0800 Subject: [PATCH] Stop the world around assignments to `tstate->eval_frame` --- Python/perf_trampoline.c | 14 +++++++++++--- Python/pystate.c | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Python/perf_trampoline.c b/Python/perf_trampoline.c index f144f7d436fe68d..22921876d1654dc 100644 --- a/Python/perf_trampoline.c +++ b/Python/perf_trampoline.c @@ -471,6 +471,14 @@ _PyPerfTrampoline_SetCallbacks(_PyPerf_Callbacks *callbacks) return 0; } +static void +set_eval_frame(PyThreadState *tstate, _PyFrameEvalFunction eval_frame) +{ + _PyEval_StopTheWorld(tstate->interp); + tstate->interp->eval_frame = eval_frame; + _PyEval_StartTheWorld(tstate->interp); +} + int _PyPerfTrampoline_Init(int activate) { @@ -484,11 +492,11 @@ _PyPerfTrampoline_Init(int activate) return -1; } if (!activate) { - tstate->interp->eval_frame = NULL; + set_eval_frame(tstate, NULL); perf_status = PERF_STATUS_NO_INIT; } else { - tstate->interp->eval_frame = py_trampoline_evaluator; + set_eval_frame(tstate, py_trampoline_evaluator); if (new_code_arena() < 0) { return -1; } @@ -514,7 +522,7 @@ _PyPerfTrampoline_Fini(void) } PyThreadState *tstate = _PyThreadState_GET(); if (tstate->interp->eval_frame == py_trampoline_evaluator) { - tstate->interp->eval_frame = NULL; + set_eval_frame(tstate, NULL); } if (perf_status == PERF_STATUS_OK) { trampoline_api.free_state(trampoline_api.state); diff --git a/Python/pystate.c b/Python/pystate.c index 24ee73c145cbccd..a2a356d8b61b64f 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2842,7 +2842,9 @@ _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, } #endif RARE_EVENT_INC(set_eval_frame_func); + _PyEval_StopTheWorld(interp); interp->eval_frame = eval_frame; + _PyEval_StartTheWorld(interp); }