Skip to content

Ensure async fixture setup and teardown run in the same task #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

provinzkraut
Copy link
Contributor

@provinzkraut provinzkraut commented Aug 17, 2025

An attempt to fix #1191.

Inspired by anyio's pytest plugin: https://github.com/agronholm/anyio/blob/b8e91a5cfe35c3b28f91ea8251674a871a7f4e26/src/anyio/_backends/_asyncio.py#L2222-L2239.

Unfortunately, this requires some hacky hacks abusing backports.asyncio.runner to ensure task context is properly propagated on Python < 3.11, in order not to break things.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 93.18182% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.28%. Comparing base (3ab1fea) to head (7581198).

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 93.18% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1193      +/-   ##
==========================================
+ Coverage   88.91%   89.28%   +0.36%     
==========================================
  Files           2        2              
  Lines         406      448      +42     
  Branches       44       48       +4     
==========================================
+ Hits          361      400      +39     
- Misses         35       37       +2     
- Partials       10       11       +1     

☔ View full report in Codecov by Sentry.
📢 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.

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.

Creating anyio.CancelScope in async fixture raises Attempted to exit cancel scope in a different task than it was entered in
2 participants