Repository navigation
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
tjkuson
requested changes
Oct 28, 2026
tjkuson
left a comment
Member
There was a problem hiding this comment.
Thank you for the contribution! The fix looks good to me, I just have a nitpick about the changelog.
Author
|
updated, thanks! |
tjkuson
approved these changes
Oct 30, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the current runner behavior reported in #1591.
When an async fixture's setup or teardown raises
CancelledError, it currently escapes into pytest as aBaseException. This can prevent setup errors from being cached correctly and can stop remaining fixture finalizers during teardown.This converts those cancellations to
PytestAsyncioErrorwhile preserving the originalCancelledErroras 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