Skip to content

Event loop shutdown failures on 0.17 #257

Closed
@zanieb

Description

@zanieb

We use the following to define a session scoped event loop (per #68)

@pytest.fixture(scope="session")
def event_loop(request):
    """
    Redefine the event loop to support session/module-scoped fixtures;
    see https://github.com/pytest-dev/pytest-asyncio/issues/68
    """
    policy = asyncio.get_event_loop_policy()
    loop = policy.new_event_loop()

    try:
        yield loop
    finally:
        loop.close()

After upgrading to 0.17, our test suite fails with

_______________________________________________________________________________________ ERROR at teardown of test_XXXX_______________________________________________________________________________________

fixturedef = <FixtureDef argname='event_loop' scope='session' baseid='tests'>, request = <SubRequest 'event_loop' for <Function test_secret_settings_are_not_serialized>>

    @pytest.hookimpl(trylast=True)
    def pytest_fixture_post_finalizer(fixturedef, request):
        """Called after fixture teardown"""
        if fixturedef.argname == "event_loop":
            policy = asyncio.get_event_loop_policy()
            # Clean up existing loop to avoid ResourceWarnings
>           policy.get_event_loop().close()

/opt/homebrew/Caskroom/miniconda/base/envs/orion-dev-38/lib/python3.8/site-packages/pytest_asyncio/plugin.py:163: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0x7ff8eaa8f8b0>

    def get_event_loop(self):
        """Get the event loop for the current context.
    
        Returns an instance of EventLoop or raises an exception.
        """
        if (self._local._loop is None and
                not self._local._set_called and
                isinstance(threading.current_thread(), threading._MainThread)):
            self.set_event_loop(self.new_event_loop())
    
        if self._local._loop is None:
>           raise RuntimeError('There is no current event loop in thread %r.'
                               % threading.current_thread().name)
E           RuntimeError: There is no current event loop in thread 'MainThread'.

/opt/homebrew/Caskroom/miniconda/base/envs/orion-dev-38/lib/python3.8/asyncio/events.py:639: RuntimeError

Modifying the fixture to set the loop with the policy at the end resolves the error

    try:
        yield loop
    finally:
        loop.close()

    policy.set_event_loop(loop)

Leaving the loop open or setting the loop with the policy before yielding does not resolve the error.

I'm not sure why this is happening now, not sure if it needs a fix here.

We're using asyncio_mode = auto.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions