From 5f997f9201eac2db9ab755445a3e6269c4e01df6 Mon Sep 17 00:00:00 2001 From: rgfegegeegege Date: Sat, 17 Jan 2026 16:07:00 +0100 Subject: [PATCH] Update perf_jit_trampoline.c --- Python/perf_jit_trampoline.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Python/perf_jit_trampoline.c b/Python/perf_jit_trampoline.c index 0ffa906d85cc6b..6be0515bb76161 100644 --- a/Python/perf_jit_trampoline.c +++ b/Python/perf_jit_trampoline.c @@ -1174,6 +1174,15 @@ static void perf_map_jit_write_entry(void *state, const void *code_addr, } } + /* Acquire lock to protect against concurrent fini() */ + PyThread_acquire_lock(perf_jit_map_state.map_lock, WAIT_LOCK); + + /* Double-check in case fini() closed it while we were waiting */ + if (perf_jit_map_state.perf_map == NULL) { + PyThread_release_lock(perf_jit_map_state.map_lock); + return; + } + /* * Extract function information from Python code object * @@ -1315,6 +1324,7 @@ static void perf_map_jit_write_entry(void *state, const void *code_addr, /* Clean up allocated memory */ PyMem_RawFree(perf_map_entry); + PyThread_release_lock(perf_jit_map_state.map_lock); } // ============================================================================= @@ -1346,12 +1356,12 @@ static int perf_map_jit_fini(void* state) { */ if (perf_jit_map_state.perf_map != NULL) { PyThread_acquire_lock(perf_jit_map_state.map_lock, 1); - fclose(perf_jit_map_state.perf_map); // This also flushes buffers + fclose(perf_jit_map_state.perf_map); + perf_jit_map_state.perf_map = NULL; // Move inside lock PyThread_release_lock(perf_jit_map_state.map_lock); /* Clean up synchronization primitive */ PyThread_free_lock(perf_jit_map_state.map_lock); - perf_jit_map_state.perf_map = NULL; } /*