Fix unclosed event loop#1430
Merged
seifertm merged 1 commit intopytest-dev:mainfrom May 1, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1430 +/- ##
==========================================
- Coverage 94.56% 94.50% -0.06%
==========================================
Files 2 2
Lines 515 510 -5
Branches 62 62
==========================================
- Hits 487 482 -5
Misses 22 22
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
seifertm
approved these changes
May 1, 2026
Contributor
seifertm
left a comment
There was a problem hiding this comment.
Thanks for the detailed explanation. We should absolutely get rid of any remaining use of get_event_loop .
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.
pytest-asyncio temporarily switches the event loop policy whilst setting up managed tests and fixtures. Before this change, that policy switch also called
asyncio.get_event_loop()to save the current loop and restore it later.asyncio.get_event_loop()can create a loop when none is set, meaning pytest-asyncio recorded the newly created loop as if it were a pre-existing one, restored it after the runner finished, and did not close it. A later test callingasyncio.run()could clear the current loop, allowing that loop to be collected and reported asResourceWarning: unclosed event loop.This makes
_temporary_event_loop_policyonly swap and restore the policy. The scoped runner fixture still installs the runner loop explicitly for custom loop factories, whereasyncio.Runner(loop_factory=...)does not do so itself, and clears it during teardown.By returning
asyncio.get_event_loop()to its original behaviour after an async test, it now means that any subsequent sync tests will see a closed event loop. I think this likely aligns with how users would expect pytest-asyncio to manage the event loop.Closes #724