Conversation
Restore critical sections *after* releasing the stop-the-world mutex, rather than before. This prevents deadlocks if there is a pinned mutex, e.g. as done by type_lock_prevent_release().
nascheme
requested review from
FFY00,
ZeroIntensity and
ericsnowcurrently
as code owners
October 3, 2026 19:28
Member
Author
|
CC @colesbury this could use your expert review. I explored a few different fixes for this deadlock and this seems like the best approach. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the free-threaded build, a thread that holds a critical section and requests a stop-the-world (STW) pause can deadlock against a thread that waits for STW while holding a lock that is not released on detach.
An example of a deadlock:
_PyThreadState_Attach()resumes T1's critical section and blocks on TYPE_LOCKThis PR fixes the deadlock by resuming the critical section after the STW mutex is released. With this change, T1 will block reacquiring TYPE_LOCK, letting T2 complete its STW work first.
This is a change in behavior of the STW APIs. On the contended path,
stop_the_world()already released the requester's critical sections (parking detaches). After this PR, they are released untilstart_the_world()returns rather than being re-acquired whenstop_the_world()returns. This is observable to code that takes a critical section itself, calls STW directly, and relies on continuous ownership after the restart.Note: this PR was created with the assistance of AI agents (Claude and GPT models).