Skip to content

Fix runner setup order for async tests - #1537

Draft
deepakganesh78 wants to merge 1 commit into
pytest-dev:mainfrom
deepakganesh78:fix/issue818-runner-fixture-order
Draft

Fix runner setup order for async tests#1537
deepakganesh78 wants to merge 1 commit into
pytest-dev:mainfrom
deepakganesh78:fix/issue818-runner-fixture-order

Conversation

@deepakganesh78

Copy link
Copy Markdown

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 replacing pytest_asyncio.plugin.Runner from a sync fixture; on main, 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 of fixturenames. 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 fixturenames so 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.
  • Regression check with the fix reverted: tests/test_set_event_loop.py::test_runner_encompasses_sync_fixtures failed with RuntimeError: 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_loop errors with PytestAssertRewriteWarning: Module already imported so cannot be rewritten; pytest_asyncio and 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.

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 tjkuson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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.

Weird interaction with pytest-mock: not requesting event_loop fixture leads to errors

2 participants