RuntimeError: Event loop is closed (pytest-asyncio 0.15.0+)

See original GitHub issue

Environment

OS: Ubuntu 20.04.2 LTS pytest version: 6.2.4 Python and pytest-asyncio version: see table below

Issue description

I’ve noticed a suite of unit tests failing with a specific combination of Python and pytest-asyncio versions. Here’s an example test run with multiple combinations, some of them failing: https://github.com/golemfactory/yapapi/actions/runs/813919338

Here’s a table describing my observations (🟢 means the faulty test passed, 🔴 means it failed):

Python version / pytest-asyncio version 0.14.0 0.15.0 0.15.1
3.6 🟢 🔴 🔴
3.7 🟢 🔴 🔴
3.8 🟢 🟢 🟢
3.9 🟢 🟢 🟢

A here’s the stack trace for the failures mentioned above:

    @pytest.hookimpl(hookwrapper=True)
    def pytest_fixture_setup(fixturedef, request):
        """Adjust the event loop policy when an event loop is produced."""
        if fixturedef.argname == "event_loop":
            outcome = yield
            loop = outcome.get_result()
            policy = asyncio.get_event_loop_policy()
            old_loop = policy.get_event_loop()
            if old_loop is not loop:
                old_loop.close()
>           policy.set_event_loop(loop)

../../../.virtualenvs/yapapi-3.6/lib/python3.6/site-packages/pytest_asyncio/plugin.py:97:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib/python3.6/asyncio/unix_events.py:1060: in set_event_loop
    self._watcher.attach_loop(loop)
/usr/lib/python3.6/asyncio/unix_events.py:815: in attach_loop
    self._do_waitpid_all()
/usr/lib/python3.6/asyncio/unix_events.py:885: in _do_waitpid_all
    self._do_waitpid(pid)
/usr/lib/python3.6/asyncio/unix_events.py:919: in _do_waitpid
    callback(pid, returncode, *args)
/usr/lib/python3.6/asyncio/unix_events.py:222: in _child_watcher_callback
    self.call_soon_threadsafe(transp._process_exited, returncode)
/usr/lib/python3.6/asyncio/base_events.py:641: in call_soon_threadsafe
    self._check_closed()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_closed(self):
        if self._closed:
>           raise RuntimeError('Event loop is closed')
E           RuntimeError: Event loop is closed

/usr/lib/python3.6/asyncio/base_events.py:381: RuntimeError

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AIGeneratedUsernamecommented, Sep 2, 2021

pytest-asyncio 0.15.1 Python 3.9 I always get the following at the end of tests execution:

Unable to properly close all Redis instances: Event loop is closed
Task was destroyed but it is pending!
task: <Task pending name='Task-11' coro=<RedisConnection._read_data() running at /usr/local/lib/python3.8/site-packages/aioredis/connection.py:186> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f4b6fa28b50>()]> cb=[RedisConnection.__init__.<locals>.<lambda>() at /usr/local/lib/python3.9/site-packages/aioredis/connection.py:168]>

Another problem is that redefining event_loop with scope="module":

# fixtures.py


@pytest.fixture(scope="module")
def event_loop() -> Generator[AbstractEventLoop, None, None]:
    loop = asyncio.get_event_loop()
    yield loop
    loop.close()

@pytest.fixture(scope="module")
async def api() -> AsyncGenerator[RestfulApi, None]:
    api = RestfulApi()
    await api.open_session()
    yield api
    await api.close_session()

leads to the error:

../../venv/lib/python3.9/site-packages/motor/metaprogramming.py:73: in method
    return framework.run_on_executor(loop,
../../venv/lib/python3.9/site-packages/motor/frameworks/asyncio/__init__.py:73: in run_on_executor
    return loop.run_in_executor(
/usr/lib/python3.9/asyncio/base_events.py:801: in run_in_executor
    self._check_closed()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_closed(self):
        if self._closed:
>           raise RuntimeError('Event loop is closed')
E           RuntimeError: Event loop is closed

/usr/lib/python3.9/asyncio/base_events.py:510: RuntimeError

when I run my tests.

1reaction
chrisk314commented, Aug 31, 2021

Hi, I’m also encountering this issue on Ubuntu 20.04. I have a suite of multiple tests. Each time I run any combination of the tests, the last test always fails with this error when cleaning up a session fixture which uses loop.run_until_complete. I am observing this problem with both python 3.7 and python 3.8, both on pytest 6.2.4 and pytest-asyncio 0.15.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pytest-asyncio has a closed event loop, but only when running ...
However when I run all tests (pytest at the project root), it fails every time with a runtime error, saying the loop is...
Read more >
pytest fails while using version asyncio-0.15.0 #209 - GitHub
Then when pytest-asyncio takes over the event loop management the error pops out. ... loop is closed') RuntimeError: Event loop is closed.
Read more >
pytest-asyncio - PyPI
It facilitates testing of code that uses the asyncio library. Note that test classes subclassing the standard unittest library are not supported.
Read more >
asyncio runtimeerror event loop is closed - You.com | The AI ...
Issue description. I've noticed a suite of unit tests failing with a specific combination of Python and pytest-asyncio versions. Here's an example test ......
Read more >
Bountysource
RuntimeError : Event loop is closed (pytest-asyncio 0.15.0+)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found