Skip to content

gh-152330: Make perf_trampoline thread safe.#152331

Open
YukaSadaoka wants to merge 5 commits into
python:mainfrom
YukaSadaoka:trampoline
Open

gh-152330: Make perf_trampoline thread safe.#152331
YukaSadaoka wants to merge 5 commits into
python:mainfrom
YukaSadaoka:trampoline

Conversation

@YukaSadaoka

Copy link
Copy Markdown

Summary of Changes

Fixes multiple critical data races, post-fork deadlocks, memory leaks, and edge-case exception handling issues in perf_trampoline.c under Free Threading and Python 3.14.

  • Cold-Path Mutex (perf_trampoline_mutex): Introduced a dedicated mutex to guard global state mutations and JIT arena allocations (compile_trampoline(), initialization, and teardown). Hot-path executions remain lock-free.
  • Atomic State Management: Converted reads and writes for perf_status, extra_code_index, and persist_after_fork to atomic operations.
  • Safe Teardown Fallback: Updated py_trampoline_evaluator to load extra_code_index atomically. If deactivation has started (returning -1), evaluating threads cleanly bypass the JIT trampoline and fall back to _PyEval_EvalFrameDefault instead of hitting assertion failures.
  • Non-Blocking I/O: Decoupled map file writing (write_state writing to /tmp/perf-PID.map) from the critical section, ensuring slow disk I/O does not hold the compiler lock.
  • Deadlock Prevention: Added a post-fork child handler to force-reset perf_trampoline_mutex to an unlocked state, preventing deadlocks if a thread forks while another thread holds the lock.
  • Graceful Child Teardown: If trampoline re-initialization fails post-fork in a child process, the exception is cleared (PyErr_Clear()) and the trampoline is disabled gracefully rather than leaking unhandled exceptions or crashing.
  • Watcher ID Sentinel Fix: Fixed a sentinel collision in pycore_ceval_state.h by initializing the global watcher ID sentinel to -1 instead of 0. This allows 0 to be correctly recognized as a valid active watcher ID.

Fixes gh-152330

@bedevere-app

bedevere-app Bot commented Jun 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Jun 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Jun 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@YukaSadaoka YukaSadaoka changed the title [Free Threading] Make perf_trampoline thread safe. gh-152330: [Free Threading] Make perf_trampoline thread safe. Jun 26, 2026
@bedevere-app

bedevere-app Bot commented Jun 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland StanFromIreland changed the title gh-152330: [Free Threading] Make perf_trampoline thread safe. gh-152330: Make perf_trampoline thread safe. Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make perf_trampoline thread safe.

1 participant