Skip to content

🐛 fix(api): keep live threads' holds at exit - #964

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/exit-spares-live-threads
Nov 17, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/exit-spares-live-threads

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

#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__ raised TypeError on construction. The last two have kept tox-dev:main red since #933 merged. Once this merges, all three are fixed.

CPython runs atexit handlers while daemon threads still run (_Py_Finalize calls 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 with thread_local=False keeps 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 StrictSoftFileLock claim stays until force_break(). The concepts page and the release() docstring now describe what exit releases.

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.
@gaborbernat
gaborbernat merged commit 7aebe2f into tox-dev:main Nov 17, 2026
44 of 45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant