Skip to content

Fix unclosed event loop#1430

Merged
seifertm merged 1 commit intopytest-dev:mainfrom
tjkuson:fix-unclosed-event-loop
May 1, 2026
Merged

Fix unclosed event loop#1430
seifertm merged 1 commit intopytest-dev:mainfrom
tjkuson:fix-unclosed-event-loop

Conversation

@tjkuson
Copy link
Copy Markdown
Contributor

@tjkuson tjkuson commented Apr 30, 2026

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 calling asyncio.run() could clear the current loop, allowing that loop to be collected and reported as ResourceWarning: unclosed event loop.

This makes _temporary_event_loop_policy only swap and restore the policy. The scoped runner fixture still installs the runner loop explicitly for custom loop factories, where asyncio.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

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.50%. Comparing base (af62924) to head (bbe8458).

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.
📢 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.

@tjkuson tjkuson marked this pull request as ready for review April 30, 2026 13:24
Copy link
Copy Markdown
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 the detailed explanation. We should absolutely get rid of any remaining use of get_event_loop .

@seifertm seifertm added this pull request to the merge queue May 1, 2026
Merged via the queue into pytest-dev:main with commit 31772d2 May 1, 2026
22 checks passed
@tjkuson tjkuson deleted the fix-unclosed-event-loop branch May 1, 2026 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResourceWarning: unclosed event loop

3 participants