Skip to content

Allow event loop policies that reject set_event_loop (#1233) - #1625

Open
krishashah-03 wants to merge 2 commits into
pytest-dev:mainfrom
krishashah-03:fix-1233-policy-rejects-set-event-loop
Open

krishashah-03 wants to merge 2 commits into
pytest-dev:mainfrom
krishashah-03:fix-1233-policy-rejects-set-event-loop

Conversation

@krishashah-03

@krishashah-03 krishashah-03 commented Nov 16, 2026 •

Copy link
Copy Markdown

Related to #1233.

What

_scoped_runner no longer fails when the active event loop policy refuses
asyncio.set_event_loop().

Why

When no loop_factory is configured, asyncio.Runner calls
asyncio.set_event_loop() on setup. Policies that manage the main-thread loop
themselves, such as GLibEventLoopPolicy, raise
RuntimeError: Thread already has a main context, so every async test errors
at fixture setup.

Change

  • Always pass the runner an explicit factory (asyncio.new_event_loop when the
    user has not configured one), so the runner doesn't call set_event_loop().
  • Try _set_event_loop() ourselves and tolerate a RuntimeError. The loop still
    runs the test, and asyncio.get_running_loop() works inside coroutines.
  • Only reset the loop on teardown if it was set.

asyncio.new_event_loop is used instead of get_event_loop_policy(), which is
deprecated in 3.14 and removed in 3.16.

Testing

  • New test test_runner_works_with_policy_that_rejects_setting_loop in
    tests/test_set_event_loop.py. It fails on main with the error above and
    passes with this change.
  • python -m pytest tests/test_set_event_loop.py: all pass except
    test_asyncio_run_after_async_fixture_does_not_leak_loop, which fails the same
    way on unmodified main in my environment (pytest 9.2.0,
    PytestAssertRewriteWarning).

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.32%. Comparing base (be39d14) to head (707fbf9).

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 57.14% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1625      +/-   ##
==========================================
- Coverage   94.86%   94.32%   -0.54%     
==========================================
  Files           2        2              
  Lines         506      511       +5     
  Branches       60       59       -1     
==========================================
+ Hits          480      482       +2     
- Misses         21       23       +2     
- Partials        5        6       +1     

☔ View full report in Codecov by Harness.
📢 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.

This branch has not been deployed

No deployments
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.

2 participants