Skip to content

Continue teardown after finalizers raise BaseExceptionGroup - #15331

Draft
0718lol wants to merge 1 commit into
pytest-dev:mainfrom
0718lol:fix/15329-teardown-base-exception-group
Draft

0718lol wants to merge 1 commit into
pytest-dev:mainfrom
0718lol:fix/15329-teardown-base-exception-group

Conversation

@0718lol

@0718lol 0718lol commented Nov 3, 2026

Copy link
Copy Markdown

Related work: #15330 addresses the same issue and was discovered during the final duplicate check. This is an independently prepared alternative, opened as a draft to make the overlap explicit rather than request duplicate review. The additional regression coverage checks GeneratorExit, finalization at module scope, preservation of the original exception object, and both fixture-owner release events in a subprocess. I am happy for maintainers to prefer #15330 or reuse any useful tests; this PR does not claim exclusive ownership of the fix.

A finalizer raising a BaseExceptionGroup containing CancelledError or GeneratorExit escapes SetupState.teardown_exact before the remaining finalizers run. A dependent function-scoped fixture can retain its cached value and be reused by the following test.

Include BaseExceptionGroup alongside the existing teardown outcomes so remaining finalizers run before the collected error is re-raised. Direct, ungrouped control-flow exceptions retain their existing behavior.

Refs #15329.

Validation on Python 3.12.15 / macOS:

  • All four new cases fail against the unchanged runner.
  • python -m pytest testing/test_runner.py testing/python/fixtures.py -q: 316 passed, 4 xfailed, 2 deprecation warnings.
  • pre-commit run --files src/_pytest/runner.py testing/test_runner.py AUTHORS changelog/15329.bugfix.rst: all applicable checks passed.
  • Unit coverage checks remaining finalizers at function and module scope and preserves the original exception object.
  • Subprocess coverage checks fresh fixture allocation and both owner releases while retaining the original teardown error.

AI assistance: OpenAI Codex helped investigate the issue, prepare this patch and run validation. The author reviewed the patch and accompanying explanation and authorized this submission.

Collect exception groups alongside ordinary teardown outcomes so remaining finalizers release their owners before the error is re-raised. Keep direct control-flow exception behavior unchanged.

Co-authored-by: OpenAI Codex <noreply@openai.com>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 3, 2026

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant