Repository navigation
π fix(api): keep __del__ off module globals at shutdown - #933
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/del-at-shutdown
branch
from
November 16, 2026 21:26
91d7ba9 to
be9a06f
Compare
gaborbernat
force-pushed
the
fix/del-at-shutdown
branch
from
November 16, 2026 22:04
be9a06f to
87a4ad3
Compare
Module teardown empties the globals the finalizer's release path reads, so a lock still referenced at exit was never released: soft markers stayed on disk and blocked the next process. Release every live lock from an atexit hook while the interpreter is intact, and let async locks release in place once exit starts. Replace the teardown test with one that exits a real interpreter holding the lock.
This was referenced Nov 17, 2026
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.
BaseFileLock.__del__calledos.getpid()andsys.is_finalizing()through module globals. When CPython finalizes a dropped lock during interpreter shutdown, module teardown may already have set those globals toNone. The finalizer then raisesAttributeError: 'NoneType' object has no attribute 'getpid', and CPython prints it as an ignored exception on every exit.The fix binds both callables as default arguments when the function is defined, the same way the standard library does in
subprocess.Popen.__del__. Everything after these two checks already runs insidecontextlib.suppress(Exception).The new test
test_del_survives_module_teardowncoversFileLockandSoftFileLock. It patchesfilelock._api.osandfilelock._api.systoNoneto imitate teardown, then calls__del__directly, as the other finalizer tests do. It fails onmainwith the traceback from the issue and passes with the fix.A
bugfixchangelog fragment is included.How I tested it (Python 3.12, local venv):
pytest -n auto tests --no-cov: 2908 passed, 134 skipped.pre-commit run --files <changed files>: ruff-check, simplint, ruff-format and flake8-lazy all pass.ty check --python .venv --output-format concise --error-on-warning .: all checks passed.Fixes #932