Skip to content

gh-151475: Fix data race in faulthandler watchdog on free-threaded builds#152123

Open
timurmamedov1 wants to merge 1 commit into
python:mainfrom
timurmamedov1:gh-151475-fix-faulthandler-thread-safety
Open

gh-151475: Fix data race in faulthandler watchdog on free-threaded builds#152123
timurmamedov1 wants to merge 1 commit into
python:mainfrom
timurmamedov1:gh-151475-fix-faulthandler-thread-safety

Conversation

@timurmamedov1

Copy link
Copy Markdown

dump_traceback_later() and cancel_dump_traceback_later() mutate the watchdog state in _PyRuntime.faulthandler.thread without synchronization. On free-threaded builds, concurrent calls corrupt the cancel_event/running lock handshake, since two threads can both allocate cancel_event (leaking one lock), or release a lock they don't hold, causing a fatal abort.

Fix by adding a PyMutex to _faulthandler_runtime_state and acquiring it in both dump_traceback_later and cancel_dump_traceback_later entry points. The mutex is acquired after argument validation (which may call into Python) but before any shared state access.

These are not hot paths and none run in signal-handler context, so a simple mutex is sufficient.

Copilot AI review requested due to automatic review settings June 24, 2026 19:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

…ded builds

Add a PyMutex to serialize dump_traceback_later() and
cancel_dump_traceback_later() calls.  Without this, concurrent
arm/cancel from multiple threads corrupts the cancel_event/running
lock handshake, causing an abort from unlocking an unheld lock.
@timurmamedov1 timurmamedov1 force-pushed the gh-151475-fix-faulthandler-thread-safety branch from 1923738 to 5014d9f Compare June 24, 2026 20:00
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.

2 participants