Fix runner setup order for async tests - #1537
Draft
deepakganesh78 wants to merge 1 commit into
Draft
Conversation
Ensure pytest-asyncio's runner fixture is set up before user fixtures for async tests so fixture monkeypatching cannot interfere with event loop creation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
tjkuson
requested changes
Aug 3, 2026
tjkuson
left a comment
Contributor
There was a problem hiding this comment.
I've moved the PR to draft as tests are failing. You are welcome to re-open the PR if you manage to get a solution with passing tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #818
Reproduction
A synchronous fixture can monkeypatch event-loop creation state before pytest-asyncio creates its runner for an async test. The added regression test simulates the reported
pytest-mock/socket lifecycle problem by replacingpytest_asyncio.plugin.Runnerfrom a sync fixture; onmain, the runner is constructed while the fixture is active and the test errors.Root cause
PytestAsyncioFunction.setup()appended the scoped runner fixture to the end offixturenames. Pytest therefore set up user fixtures first and set up/teared down the runner inside those fixtures' lifetime.Fix
Insert the runner fixture at the front of
fixturenamesso the event loop runner encompasses synchronous fixtures used by async tests. This preserves normal async test behavior while preventing user fixture monkeypatching from interfering with event loop creation.Compatibility notes
This only changes fixture ordering for pytest-asyncio's internal runner fixture. No public API or configuration changes are introduced.
Validation
python -m pytest tests/test_set_event_loop.py::test_runner_encompasses_sync_fixtures -q: 1 passed.tests/test_set_event_loop.py::test_runner_encompasses_sync_fixturesfailed withRuntimeError: Runner constructed while user fixture is active.python -m pytest tests/test_set_event_loop.py -q: 61 passed, 1 failed. The failure is pre-existing on this Windows/Python 3.13 + pytest 9.1.1 environment:test_asyncio_run_after_async_fixture_does_not_leak_looperrors withPytestAssertRewriteWarning: Module already imported so cannot be rewritten; pytest_asyncioand reproduces with the runner-order fix reverted.python -m pytest: 290 passed, 3 skipped, 6 failed. The failures are the same pre-existing Windows/Python 3.13 + pytest 9.1.1 subprocess/in-process loop/assert-rewrite failures and are unrelated to this change.pre-commit run --files pytest_asyncio/plugin.py tests/test_set_event_loop.py changelog.d/818.fixed.rst: passed.