Skip to content

Properly configure async tests #17311

Open

Description

@parthea

A recent release of pytest-asyncio (v1.4.0) caused several tests to fail with RuntimeError: There is no current event loop in thread 'MainThread' (see comment here).

As a quick workaround, PR #17260 introduced a temporary autouse fixture to manually manage the event loop:

@pytest.fixture(autouse=True)
def set_event_loop():
    try:
        asyncio.get_running_loop()
        yield
    except RuntimeError:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        try:
            yield
        finally:
            loop.close()
            asyncio.set_event_loop(None)

The Correct Long-Term Fix

This workaround is a stopgap. The root cause is that tests containing asynchronous code are missing the proper async def declaration and the @pytest.mark.asyncio marker.

The correct long-term solution is to properly decorate and define these tests. For example, PR #17308 resolved this error (CI run logs) by properly updating the test definitions (see commit diff).

Next Steps

Please keep this issue open until the temporary set_event_loop fixture is entirely removed from the codebase and all affected tests are properly refactored with async def and @pytest.mark.asyncio.

Activity

  1. added
    priority: p2
    type: process
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2type: process

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions