Skip to content

Report cancelled async fixture setup/teardown as fixture errors - #1592

Open
bhabishnu wants to merge 2 commits into
pytest-dev:mainfrom
bhabishnu:fix-cancelled-fixture-teardown
Open

bhabishnu wants to merge 2 commits into
pytest-dev:mainfrom
bhabishnu:fix-cancelled-fixture-teardown

Conversation

@bhabishnu

Copy link
Copy Markdown

Fixes the current runner behavior reported in #1591.

When an async fixture's setup or teardown raises CancelledError, it currently escapes into pytest as a BaseException. This can prevent setup errors from being cached correctly and can stop remaining fixture finalizers during teardown.

This converts those cancellations to PytestAsyncioError while preserving the original CancelledError as the cause. It also ensures ContextVars changed by async-generator fixtures are restored when teardown raises.

The change only affects the current runner; the experimental runner in #1579 remains separate.

Closes #1591

CancelledError raised during async fixture setup or teardown escaped
into pytest as a BaseException, so a cancelled setup was not cached
for later tests and a cancelled teardown prevented the remaining
finalizers from running. Raise a PytestAsyncioError chained to the
CancelledError instead.

Also reset ContextVars changed by async generator fixtures in a
finally block, so they are restored when teardown raises.

Closes pytest-dev#1591
@codecov-commenter

codecov-commenter commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.54%. Comparing base (9830071) to head (86b3d3c).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1592      +/-   ##
==========================================
+ Coverage   94.50%   94.54%   +0.03%     
==========================================
  Files           2        2              
  Lines         510      513       +3     
  Branches       62       61       -1     
==========================================
+ Hits          482      485       +3     
  Misses         22       22              
  Partials        6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tjkuson tjkuson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! The fix looks good to me, I just have a nitpick about the changelog.

Comment thread changelog.d/1591.fixed.rst Resolved
@bhabishnu

Copy link
Copy Markdown
Author

updated, thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CancelledError from async fixtures can skip fixture cleanup

3 participants