RuntimeError: Event loop is closed (pytest-asyncio 0.15.0+)
See original GitHub issueEnvironment
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:
- Created 2 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
pytest-asyncio 0.15.1 Python 3.9 I always get the following at the end of tests execution:
Another problem is that redefining
event_loopwithscope="module":leads to the error:
when I run my tests.
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.