Skip to content

Regression in 0.25.2 - Event loop is closed #1040

@schlamar

Description

@schlamar

If there is a user defined fixture which handles loop shutdown and closing the loop on its own, the plugin raises an error.

Expected behavior: If user code closes the event loop, pytest-asyncio should handle this transparently.

Actual behavior: pytest-asyncio raises RuntimeError: Event loop is closed.

..\..\AppData\Local\Programs\Python\Python310\lib\contextlib.py:142: in __exit__
    next(self.gen)
.venv\lib\site-packages\pytest_asyncio\plugin.py:1168: in _provide_event_loop
    loop.run_until_complete(loop.shutdown_asyncgens())
..\..\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py:624: in run_until_complete
    self._check_closed()

Example working with 0.25.1, crashes with 0.25.2:

import asyncio

import pytest


@pytest.fixture(autouse=True)
def handle_event_loop(request: pytest.FixtureRequest):
    event_loop = None
    if "event_loop" in request.fixturenames:
        event_loop = request.getfixturevalue("event_loop")

    yield

    if event_loop is None or event_loop.is_closed():
        return
    try:
        asyncio.runners._cancel_all_tasks(event_loop)
        event_loop.run_until_complete(event_loop.shutdown_asyncgens())
        if hasattr(event_loop, "shutdown_default_executor"):  # Python 3.9+
            event_loop.run_until_complete(event_loop.shutdown_default_executor())
    finally:
        event_loop.close()


async def test():
    assert True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy