-
Notifications
You must be signed in to change notification settings - Fork 152
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
ResourceWarning: unclosed event loop #724
Comments
full demo here kevin1024/vcrpy@d009336 more minimal reproducer here https://github.com/graingert/asyncio_unclosed_loop |
I repeated this with e1415c1 |
I have the same issue in versions 0.22.0+, including 0.23.0a0. I first spent a ton of time trying to hunt down a new
I couldn't for the life of me figure out where I had an unclosed socket. So I finally admitted defeat and silenced the
The results seemed to change depending on how many tests I was running in my test suite. Downgrading to 0.21.1 fixed both problems. |
Thanks for the report! I can reproduce the issue with pytest-asyncio v0.23.2. I can also reproduce it with pytest-asyncio v0.21.1, though: $ python -X dev -m pytest --setup-show
===== test session starts =====
platform linux -- Python 3.12.1, pytest-7.4.3, pluggy-1.3.0
rootdir: /tmp/tst
plugins: asyncio-0.21.1
asyncio: mode=Mode.STRICT
collected 2 items
test_a.py
SETUP F event_loop
SETUP F demo (fixtures used: event_loop)
test_a.py::test_aiohttp_test_client_json (fixtures used: demo, event_loop, request).
TEARDOWN F demo
TEARDOWN F event_loop
test_a.py::test_redirect.
===== warnings summary =====test_a.py::test_redirect
/usr/lib/python3.12/asyncio/base_events.py:723: ResourceWarning: unclosed event loop <_UnixSelectorEventLoop running=False closed=False debug=True>
_warn(f"unclosed event loop {self!r}", ResourceWarning, source=self)
Enable tracemalloc to get traceback where the object was allocated.
See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
===== 2 passed, 1 warning in 0.02s ===== This means it's probably not directly related to the v0.23 release. Based on the console output above, my current understanding is that pytest(-asyncio) runs the async test normally. When the pytest-asyncio/pytest_asyncio/plugin.py Lines 824 to 833 in b614c77
When the sync test calls @graingert I'm aware you're involved in upstream asyncio development. Do you think it's worth filing an enhancement issue for asyncio.run (or rather asyncio.Runner) to close any existing loop before setting a new loop? @phillipuniverse If you don't use asyncio.run, it's possible that your issue has a separate cause. I can help more, if you provide a reproducer for you specific issue. |
@seifertm currently I'm trying to remove the policy system and get/set event loop in favour of loop_factory so this becomes a non-issue. I think to fix this instead of using _provide_clean_event_loop you should call |
@seifertm I ran into this problem again and now realize that I do indeed have the same problem as @graingert with Maybe 1 more data point - the issue for me only popped up in Python 3.12.2. I had a test suite that ran fine with Python 3.11.4 and when I went to 3.12.2 I got the failure. I ended up hunting it down to executing Alembic migrations which was super non-obvious. [The recommended approach]9https://alembic.sqlalchemy.org/en/latest/cookbook.html#using-asyncio-with-alembic) for asyncio with alembic involves using It makes me think there is some sort of timing thing going on too, like maybe the event loop has to hang around for long enough such that it exhibits this problem, maybe some sort of race condition somewhere. |
using pytest-asyncio asyncio 0.23.2
results in
The text was updated successfully, but these errors were encountered: