Repository navigation
🐛 fix(api): keep live threads' holds at exit - #964
Merged
gaborbernat merged 1 commit intoNov 17, 2026
Merged
Conversation
The exit handler from tox-dev#933 released every live lock, including one a daemon thread was still using, and hung when a daemon thread was parked in fork bookkeeping. It also needed hashable locks. Release only the main thread's holds and holds of exited threads, and a shared lock only when no other thread is alive. Let just the exiting thread skip fork bookkeeping, so daemon threads and their children keep it. Track live locks by identity.
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.
#933 added an exit handler that releases every lock still alive, and it broke three things. 🔒 A daemon thread still inside
with lock:at exit lost its lock to another process while it kept writing. An exit hung for good when a daemon thread was parked inside filelock's fork bookkeeping, the registry that lets a forked child close the lock descriptors it inherits. A lock subclass that defines__eq__without__hash__raisedTypeErroron construction. The last two have kepttox-dev:mainred since #933 merged. Once this merges, all three are fixed.CPython runs
atexithandlers while daemon threads still run (_Py_Finalizecalls them before it stops other threads), so the handler now releases only holds no running thread is inside: the main thread's, and those of threads that already ended. A lock built withthread_local=Falsekeeps a single hold and cannot tell which thread took it, so the handler leaves it held whenever any other thread is alive, even when the main thread took it. The handler tracks live locks by identity rather than by hash.While the exit release runs, its own thread skips the fork bookkeeping, so it never waits on a daemon thread parked there. Daemon threads still running at that point, and children they fork, keep the bookkeeping, so a fork during exit still closes the lock descriptors the child inherits.
Behavior someone would notice: a lock a live daemon thread holds at exit stays held until the process ends, as before #933. The OS then drops native locks, soft lock markers stay for contenders to reclaim, and a
StrictSoftFileLockclaim stays untilforce_break(). The concepts page and therelease()docstring now describe what exit releases.