Skip to content
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

Prevent premature closing of scoped event loops #714

Merged

Conversation

seifertm
Copy link
Contributor

@seifertm seifertm commented Dec 7, 2023

Fixes a bug that caused event loops to be closed prematurely when using async generator fixtures with class scope or wider in a function-scoped test.

The fixture setup of the "event_loop" fixture closes any existing loop. The existing loop could also belong to a pytest-asyncio scoped event loop fixture. This caused async generator fixtures using the scoped loop to raise a RuntimeError on teardown, because the scoped loop was closed before the fixture finalizer could not be run. In fact, everything after the async generation fixture's "yield" statement had no functioning event loop.

The issue was addressed by adding a special attribute to the scoped event loops provided by pytest-asyncio. If this attribute is present, the setup code of the "event_loop" fixture will not close the loop. This allows keeping backwards compatibility for code that doesn't use scoped loops. It is assumed that the magic attribute can be removed after the deprecation period of event_loop_ fixture overrides.

Fixes #708

…en using async generator fixtures with class scope or wider in a function-scoped test.

The fixture setup of the "event_loop" fixture closes any existing loop. The existing loop could also belong to a pytest-asyncio scoped event loop fixture. This caused async generator fixtures using the scoped loop to raise a RuntimeError on teardown, because the scoped loop was closed before the fixture finalizer could not be run. In fact, everything after the async generation fixture's "yield" statement had no functioning event loop.

The issue was addressed by adding a special attribute to the scoped event loops provided by pytest-asyncio. If this attribute is present, the setup code of the "event_loop" fixture will not close the loop. This allows keeping backwards compatibility for code that doesn't use scoped loops. It is assumed that the magic attribute can be removed after the deprecation period of event_loop_ fixture overrides.

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b614c77) 94.89% compared to head (3b77ab8) 95.13%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #714      +/-   ##
==========================================
+ Coverage   94.89%   95.13%   +0.24%     
==========================================
  Files           2        2              
  Lines         470      473       +3     
  Branches       93       93              
==========================================
+ Hits          446      450       +4     
  Misses         16       16              
+ Partials        8        7       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@seifertm seifertm added this to the v0.23.3 milestone Dec 7, 2023
@seifertm seifertm added this pull request to the merge queue Dec 9, 2023
@seifertm seifertm mentioned this pull request Dec 9, 2023
Merged via the queue into pytest-dev:main with commit 9a7248a Dec 9, 2023
9 checks passed
@seifertm seifertm deleted the loop-closed-before-fixture-finalizer branch December 9, 2023 13:06
@seifertm seifertm restored the loop-closed-before-fixture-finalizer branch December 9, 2023 13:08
@seifertm seifertm deleted the loop-closed-before-fixture-finalizer branch December 24, 2023 06:04
@seifertm seifertm modified the milestones: v0.23.3, v0.23 Jan 1, 2024
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.

Loop is closed before fixture teardown completes
2 participants