Skip to content

Replace event loops with asyncio.Runner #1152

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

Merged

Conversation

seifertm
Copy link
Contributor

Previously, pytest-asyncio used a combination of low-level asyncio functions to manage event loops. This patch replaces the low-level functions with the use of asyncio.Runner.

The runner takes care of cancelling remaining tasks at the end of its scope, avoiding errors about pending tasks. This also fixes an issue that caused RuntimeErrors in abanoned tasks that called functions which depend on a running loop.

Moreover, the use of asyncio.Runner and its backport allows backporting the propagation of contextvars from fixtures to tests to Python 3.9 and Python 3.10.

Supersedes #309
Closes #127
Closes #200
Closes #878
Partially addresses #1032

seifertm added 5 commits June 24, 2025 09:50
Previously, pytest-asyncio used a combination of low-level asyncio functions to manage event loops. This patch replaces the low-level functions with the use of asyncio.Runner. The runner takes care of cancelling remaining tasks at the end of its scope, avoiding errors about pending tasks. This also fixes an issue that caused RuntimeErrors in abanoned tasks that called functions which depend on a running loop.

Moreover, the use of asyncio.Runner allows backporting the propagation of contextvars from fixtures to tests to Python 3.9 and Python 3.10.
@seifertm seifertm requested review from asvetlov and Tinche as code owners June 24, 2025 10:15
@seifertm seifertm added this to the v1.1 milestone Jun 24, 2025
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 86.48649% with 5 lines in your changes missing coverage. Please review.

Project coverage is 89.01%. Comparing base (6c9914b) to head (ce15320).

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 86.48% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1152      +/-   ##
==========================================
- Coverage   89.40%   89.01%   -0.39%     
==========================================
  Files           2        2              
  Lines         434      428       -6     
  Branches       51       51              
==========================================
- Hits          388      381       -7     
- Misses         31       33       +2     
+ Partials       15       14       -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.

@seifertm seifertm added this pull request to the merge queue Jun 25, 2025
Merged via the queue into pytest-dev:main with commit f9c5d67 Jun 25, 2025
18 of 19 checks passed
@seifertm seifertm deleted the replace-event-loops-with-asyncio-runner branch June 25, 2025 17:04
@seifertm seifertm restored the replace-event-loops-with-asyncio-runner branch June 25, 2025 17:05
@seifertm seifertm deleted the replace-event-loops-with-asyncio-runner branch June 25, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants