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.

Copy link
Contributor

@seifertm seifertm left a comment

Choose a reason for hiding this comment

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

Thanks for sharing this! In general, I think we can try something like this.

It expect this to be a very intrusive change that has the potential to break a lot. Therefore, the queue-based task runner needs to be behind a feature toggle, such as a configuration value and/or a command line option.

I'm not sure what the best approach is codewise. My very early guess it that it would help to create some kind of abstraction on top of asyncio.Runner, and move some existing log, such as handling of contextvars, there. In the next step, we can create our new queue-based implementation and eventually we can use the feature toggle to switch between them. This is just an idea, though. I have no clue how it plays out.

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
3 participants