Skip to content
Open
Changes from all commits
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
14 changes: 12 additions & 2 deletions Python/perf_jit_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}

// =============================================================================
Expand Down Expand Up @@ -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;
}

/*
Expand Down