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

Fix wrong event loop reference with mixed scoped tests #1029

Conversation

seifertm
Copy link
Contributor

Issue #950 describes a problem with tests that have mixed event loop scopes. When a function-scoped loop is executed in between two tests with wider loop scope, the tests after the function-scoped tests reference the wrong event loop.

This is caused by the implementation of the event_loop fixture and its finalizers. This PR addresses the issue twofold:

  1. It changes the implementation of the event_loop fixture so that it properly restores the current event loop policy and event loop when the fixture is torn down.
  2. It changes the event_loop fixture finalizers to only close loops that were not created by pytest-asyncio. This prevents the finalizers from accidentally closing a module-scoped loop provided by the plugin, for example.

Fixes #950

…two wider-scoped tests.

The event_loop fixture finalizers only close event loops that were not created by pytest-asyncio. This prevents the finalizers from accidentally closing a module-scoped loop, for example.
…the more generic "__pytest_asyncio" magic attribute.
@seifertm seifertm added this to the v0.25 milestone Dec 30, 2024
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 90.32258% with 3 lines in your changes missing coverage. Please review.

Project coverage is 91.10%. Comparing base (581bd29) to head (7600594).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 90.32% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1029      +/-   ##
==========================================
- Coverage   91.42%   91.10%   -0.33%     
==========================================
  Files           2        2              
  Lines         548      562      +14     
  Branches       72       74       +2     
==========================================
+ Hits          501      512      +11     
- Misses         28       30       +2     
- Partials       19       20       +1     

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

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

Looks good.
The code is extremely complex now; I have no idea how to simplify it and keep backward compatibility, though.

@seifertm
Copy link
Contributor Author

Thanks for having a look!

I agree that the code is really complex. It takes hours to debug even simple changes and understand

We really made an effort to keep backwards compatibility, so that made the situation worse. Maybe we should start following through with all the deprecations that were introduced and start cleaning up the code.

@seifertm seifertm added this pull request to the merge queue Dec 31, 2024
Merged via the queue into pytest-dev:main with commit 04f9044 Dec 31, 2024
15 checks passed
@seifertm seifertm deleted the fix-wrong-event-loop-reference-with-mixed-scoped-tests branch December 31, 2024 05:32
@seifertm seifertm restored the fix-wrong-event-loop-reference-with-mixed-scoped-tests branch December 31, 2024 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants