Skip to content

🐛 fix(api): keep __del__ off module globals at shutdown - #933

Open
kushaldave002 wants to merge 1 commit into
tox-dev:mainfrom
kushaldave002:fix/del-at-shutdown
Open

kushaldave002 wants to merge 1 commit into
tox-dev:mainfrom
kushaldave002:fix/del-at-shutdown

Conversation

@kushaldave002

Copy link
Copy Markdown

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

This branch has not been deployed

No deployments
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)

1 participant