Skip to content

Conversation

tjkuson
Copy link
Contributor

@tjkuson tjkuson commented Aug 9, 2025

Automatically reinstates the event loop on the main thread when needed.

The coverage report suggests the patch is uncovered, but I think this is due to limitations of measuring coverage across processes.

Fixes #1177

Note: this is a work-in-progress proposal based on my understanding of the issue. If the maintainers prefer a different approach or consider the issue invalid, I’m happy to adjust or close this PR.

@codecov-commenter
Copy link

codecov-commenter commented Aug 9, 2025

Codecov Report

❌ Patch coverage is 81.57895% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.47%. Comparing base (3ab1fea) to head (ac9e0d6).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 81.57% 6 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1180      +/-   ##
==========================================
- Coverage   88.91%   88.47%   -0.44%     
==========================================
  Files           2        2              
  Lines         406      434      +28     
  Branches       44       46       +2     
==========================================
+ Hits          361      384      +23     
- Misses         35       39       +4     
- Partials       10       11       +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.

@tjkuson tjkuson force-pushed the mainthread-fix branch 3 times, most recently from 5ff08a0 to bb8628a Compare August 9, 2025 15:41
Copy link
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.

@tjkuson In the issue related to this PR, you mentioned you had a local patch that satisfies the edge case I mentioned. It sounded really promising and I'd appreciate if you could share it!

_loop = _get_event_loop_no_warn()
except RuntimeError:
# Handle situation where asyncio.set_event_loop(None) removes shared loops.
_reinstate_event_loop_on_main_thread()
Copy link
Contributor

Choose a reason for hiding this comment

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

The function _get_event_loop_no_warn is a helper that suppresses a call to get_event_loop, which is effectively deprecated in favor of get_running_loop. This deprecation was one of the drivers for removing the event_loop fixture, which relied on this functionality.

The fact that wrap_in_sync still uses this is unfortunate and I hope to rectify this in #1203.

The point I'm trying to make is that wrap_in_sync seems to be the wrong place to reinstate the loop, because it would mean doubling down on the get_event_loop use.

Copy link
Contributor Author

@tjkuson tjkuson Aug 24, 2025

Choose a reason for hiding this comment

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

Hm, I think I follow. #1203 looks promising, and I'll think about how I'd rebase this fix on your PR! In the meantime, I've pushed my revision of the patch to reinstate the specific event loops with they are unset to resolve your concern mentioned the original issue (#1177 (comment)) (which, admittedly, doesn't address this new comment).

@tjkuson tjkuson force-pushed the mainthread-fix branch 3 times, most recently from 644bc9f to e9b9fb9 Compare August 24, 2025 18:20
@tjkuson
Copy link
Contributor Author

tjkuson commented Aug 24, 2025

@seifertm I've pushed the patch that satisfies the edge-case you mentioned (though the PR is still quite proof-of-concept).

@@ -49,6 +49,16 @@
PytestPluginManager,
)

_seen_markers: set[int] = set()
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this code is intended to suppress duplicate warnings about keyword arguments in the asyncio marker. This should no longer be needed, because it has been addressed in 8585040.

)
)

_SCOPE_TO_CONTEXTVAR = {
Copy link
Contributor

Choose a reason for hiding this comment

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

The current way of handling contextvars with copying them and restoring them was introduced before switching to asyncio.Runner, if I remember correctly.

My understanding is that asyncio.Runner holds a default context along with its loop. We already have one Runner per loop scope. Do you think there's any way we can utilize the Runner's default context rather than storing contexts per scope externally?

This is just something to think about. If it's not possible or it would take a lot more work, we can start with this solution as well.

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.

RuntimeError when the event loop is unset between tests
3 participants