Skip to content

πŸ› fix(api): keep __del__ off module globals at shutdown - #933

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
kushaldave002:fix/del-at-shutdown
Nov 17, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
kushaldave002:fix/del-at-shutdown

Conversation

@kushaldave002

Copy link
Copy Markdown
Contributor

BaseFileLock.__del__ called os.getpid() and sys.is_finalizing() through module globals. When CPython finalizes a dropped lock during interpreter shutdown, module teardown may already have set those globals to None. The finalizer then raises AttributeError: '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 inside contextlib.suppress(Exception).

The new test test_del_survives_module_teardown covers FileLock and SoftFileLock. It patches filelock._api.os and filelock._api.sys to None to imitate teardown, then calls __del__ directly, as the other finalizer tests do. It fails on main with the traceback from the issue and passes with the fix.

A bugfix changelog 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

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BaseFileLock.__del__ raises AttributeError at interpreter shutdown (os is None)

2 participants