[3.15] gh-149156: Fix perf trampoline crash after fork (GH-150347) (#150394)

This commit is contained in:
Miss Islington (bot) 2026-05-25 20:19:22 +02:00 committed by GitHub
parent 28037c2d11
commit e2362aac34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View file

@ -210,9 +210,8 @@ enum perf_trampoline_type {
static void free_code_arenas(void);
static void
perf_trampoline_reset_state(void)
perf_trampoline_clear_code_watcher(void)
{
free_code_arenas();
if (code_watcher_id >= 0) {
PyCode_ClearWatcher(code_watcher_id);
code_watcher_id = -1;
@ -220,6 +219,13 @@ perf_trampoline_reset_state(void)
extra_code_index = -1;
}
static void
perf_trampoline_reset_state(void)
{
free_code_arenas();
perf_trampoline_clear_code_watcher();
}
static int
perf_trampoline_code_watcher(PyCodeEvent event, PyCodeObject *co)
{
@ -621,9 +627,10 @@ _PyPerfTrampoline_AfterFork_Child(void)
// After fork, Fini may leave the old code watcher registered
// if trampolined code objects from the parent still exist
// (trampoline_refcount > 0). Clear it unconditionally before
// Init registers a new one, to prevent two watchers sharing
// the same globals and double-decrementing trampoline_refcount.
perf_trampoline_reset_state();
// Init registers a new one, but keep the old arenas mapped: the
// child may still need to return through trampoline frames that
// were on the C stack at fork().
perf_trampoline_clear_code_watcher();
_PyPerfTrampoline_Init(1);
}
}