Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in 0.25.2 - Event loop is closed #1040

Open
schlamar opened this issue Jan 8, 2025 · 3 comments
Open

Regression in 0.25.2 - Event loop is closed #1040

schlamar opened this issue Jan 8, 2025 · 3 comments
Milestone

Comments

@schlamar
Copy link

schlamar commented Jan 8, 2025

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
@seifertm
Copy link
Contributor

Thanks for the detailed report!
A patch release shouldn't introduce a breaking change. We should address this in a new v0.25 release.
It's also quite easy to address in pytest-asyncio.

@bradydean
Copy link

I am also getting this error with my doctests.

@medihack
Copy link

The error occurs already with v0.25.1 (in my case, when using it together with pytest-playwright).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants