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

RuntimeError: Runner is closed in pytest #619

Closed
2 tasks done
EdorianDark opened this issue Sep 30, 2023 · 5 comments · Fixed by #635
Closed
2 tasks done

RuntimeError: Runner is closed in pytest #619

EdorianDark opened this issue Sep 30, 2023 · 5 comments · Fixed by #635
Labels
bug Something isn't working

Comments

@EdorianDark
Copy link

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

4.0.0

Python version

3.11.5

What happened?

The error message "RuntimeError: Runner is closed" appears.

How can we reproduce the bug?

The following leads to the described error:

import pytest
pytestmark = pytest.mark.anyio

@pytest.fixture(scope="session")
def anyio_backend():
    return "asyncio"

@pytest.fixture(scope="class")
async def bbbbbb():
    yield ""
 
@pytest.fixture(scope="class")
async def aaaaaa():
    yield ""

class TestJobs(object):
    async def test_1(self, bbbbbb):
        pass

    async def test_2(self, aaaaaa, bbbbbb):
        pass

    @pytest.mark.skip
    async def test_3(self, aaaaaa, bbbbbb):
        pass

I get always the error in the skipped test

self = <asyncio.runners.Runner object at 0x101af5d50>

    def _lazy_init(self):
        if self._state is _State.CLOSED:
>           raise RuntimeError("Runner is closed")
E           RuntimeError: Runner is closed

/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py:133: RuntimeError
=================================== short test summary info ===================================
ERROR test_pytest_error.py::TestJobs::test_3 - RuntimeError: Runner is closed
============================ 2 passed, 1 skipped, 1 error in 0.04s =========================

Not that if I change it to pytestmark = pytest.mark.asyncio there is no error.
Also the error goes away if I change the order of the function parameters to have bbbbb always in front.

@EdorianDark EdorianDark added the bug Something isn't working label Sep 30, 2023
@agronholm
Copy link
Owner

Notes: The third test seems irrelevant. The second test also passes if the bbbbbb fixture is removed from it.

@agronholm
Copy link
Owner

Minimal test case with module scoped fixtures:

import pytest

pytestmark = pytest.mark.anyio


@pytest.fixture(scope="session")
def anyio_backend():
    return "asyncio"


@pytest.fixture(scope="module")
async def bbbbbb():
    yield ""


@pytest.fixture(scope="module")
async def aaaaaa():
    yield ""


async def test_1(bbbbbb):
    pass


async def test_2(aaaaaa, bbbbbb):
    pass

@agronholm
Copy link
Owner

I just ran into this problem at my day job 😅

@agronholm
Copy link
Owner

The issue is that the first fixture gets "ownership" of the test runner, and if that fixture isn't the last one to be torn down, the rest will fail with this error at teardown.

@agronholm
Copy link
Owner

@EdorianDark Let me know if you're available to review the fix PR or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants