Skip to content

Fix stale fixture finalizers after setup errors - #14970

Open
MarthalaJagruthiReddy wants to merge 1 commit into
pytest-dev:mainfrom
MarthalaJagruthiReddy:fix/fixture-finalizer-after-setup-error
Open

Fix stale fixture finalizers after setup errors#14970
MarthalaJagruthiReddy wants to merge 1 commit into
pytest-dev:mainfrom
MarthalaJagruthiReddy:fix/fixture-finalizer-after-setup-error

Conversation

@MarthalaJagruthiReddy

@MarthalaJagruthiReddy MarthalaJagruthiReddy commented Sep 4, 2026

Copy link
Copy Markdown

Summary

When a pytest_fixture_setup hook raises while setting up a parametrized fixture, FixtureDef.finish() returned early because cached_result was None. The post-finalizer remained queued, so the next parameter hit the assert not self._finalizers assertion and failed internally.

The same stale-finalizer state occurs with pytest's built-in async-fixture guard, where one unsupported async fixture can turn later tests into bare internal errors. Keep the early return only when there are no pending finalizers. This allows pending cleanup to run after setup failures and lets subsequent tests report their own results.

Fixes #14800

Tests

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 uv run --frozen pytest -o minversion=0 testing/python/fixtures.py -q (241 passed, 3 xfailed)
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 uv run --frozen pytest -o minversion=0 testing/acceptance_test.py -k 'sync_test_async_fixture or async_autouse_fixture' -q (4 passed)
  • uvx ruff check src/_pytest/fixtures.py testing/python/fixtures.py testing/acceptance_test.py
  • uvx ruff format --check src/_pytest/fixtures.py testing/python/fixtures.py testing/acceptance_test.py

Checklist

  • Include new tests or update existing tests when applicable.
  • Add text linking this PR to the issue.
  • Create a changelog file in changelog/.
  • Add yourself to AUTHORS.
  • Allow maintainers to push and squash when merging my commits.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 4, 2026
@MarthalaJagruthiReddy
MarthalaJagruthiReddy force-pushed the fix/fixture-finalizer-after-setup-error branch from b9871ae to 9f249a6 Compare September 4, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal AssertionError on stale _finalizers when a pytest_fixture_setup hookimpl raises during setup of a parametrized argument

1 participant