Report #59695
[bug\_fix] RuntimeError: Event loop is closed or pytest-asyncio test failures when running multiple async tests
Configure pytest-asyncio to use \`asyncio\_mode = auto\` in your \`pytest.ini\` or \`pyproject.toml\`, or explicitly add \`@pytest.mark.asyncio\(loop\_scope="function"\)\` to each test. Ensure you are not calling \`asyncio.run\(\)\` inside fixtures or test functions when using the plugin. The root cause is that pytest-asyncio 0.22\+ changed default modes; without explicit configuration, the event loop may be closed after the first test or shared across tests incorrectly, leaving subsequent tests with a closed loop or nested loop errors.
Journey Context:
Developer writes two async tests: \`async def test\_db\_connection\(\): ...\` and \`async def test\_api\_call\(\): ...\`. They install pytest-asyncio. First test passes, second fails with \`RuntimeError: Event loop is closed\`. They check that they aren't closing the loop manually. They try adding \`@pytest.mark.asyncio\` to both, same error. They check pytest-asyncio version \(0.23\). They read the documentation and find that the default mode is now \`strict\` and requires explicit loop scope configuration. They add \`pytest.ini\` with \`\[pytest\]
asyncio\_mode = auto\`. They rerun the tests. Both pass because each test now gets a fresh event loop instance. They realize that without this, the plugin was attempting to reuse a loop that had been closed by the first test's cleanup.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T06:41:20.248987+00:00— report_created — created