aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
15.11k stars 2.02k forks source link

Event loop is closed error #7196

Closed Joshuaalbert closed 3 weeks ago

Joshuaalbert commented 1 year ago

Describe the bug

I noticed a unittest failing.

https://github.com/aio-libs/aiohttp/actions/runs/4131520742/jobs/7139246584#step:10:474

We're finding similar bugs in our code that just popped up, and wonder if you have any idea what is causing it.

To Reproduce

Run the unittest at this line: https://github.com/aio-libs/aiohttp/actions/runs/4131520742/jobs/7139246584#step:10:474

Expected behavior

It should pass

Logs/tracebacks

E                 File "/home/runner/work/aiohttp/aiohttp/aiohttp/web_protocol.py", line 487, in _handle_request
E                   reset = await self.finish_response(request, resp, start_time)
E                 File "/home/runner/work/aiohttp/aiohttp/aiohttp/web_protocol.py", line 641, in finish_response
E                   await prepare_meth(request)
E                 File "/home/runner/work/aiohttp/aiohttp/aiohttp/web_fileresponse.py", line 286, in prepare
E                   await loop.run_in_executor(None, fobj.close)
E                 File "/opt/hostedtoolcache/Python/3.10.9/x64/lib/python3.10/asyncio/base_events.py", line 808, in run_in_executor
E                   self._check_closed()
E                 File "/opt/hostedtoolcache/Python/3.10.9/x64/lib/python3.10/asyncio/base_events.py", line 515, in _check_closed
E                   raise RuntimeError('Event loop is closed')
E               RuntimeError: Event loop is closed

Python Version

3.10.8/9

aiohttp Version

$ python -m pip show aiohttp

multidict Version

$ python -m pip show multidict

yarl Version

Name: yarl
Version: 1.8.1
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /home/albert/miniconda3/envs/tha_py/lib/python3.10/site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

Ubunutu

Related component

Client

Additional context

No response

Code of Conduct

Dreamsorcerer commented 1 year ago

Happens every few test runs, I've not been able to figure it out.

However, I just had another look and saw something very suspicious: https://github.com/aio-libs/aiohttp/blob/master/tests/test_web_sendfile_functional.py#L44

If I'm reading that correctly, the code changes the event loop in the middle of handling a request, which seems crazy. Maybe you can have a look and see if that code can be changed to not set the event loop in the middle of a running application?

Dreamsorcerer commented 1 year ago

Another: https://github.com/aio-libs/aiohttp/actions/runs/4095635910/jobs/7172398166

@Joshuaalbert You said you have the same issue in your code. Is that in tests or when running the application, and are you using run_app() to run the application?

Just found this issue: https://github.com/python/cpython/issues/74269 Suggests the cause may be due to running a loop differently. In which case the test failures we see are likely due to pytest-asyncio or similar.

Dreamsorcerer commented 1 year ago

I also note that an event loop gets closed at: https://github.com/pytest-dev/pytest-asyncio/blob/master/pytest_asyncio/plugin.py#L398 So, I think that may be related.

misuzu commented 1 year ago

Same issue, NixOS, armv7l-linux, Python 3.10.10: https://hydra.armv7l.xyz/build/4977/nixlog/43

skriesch commented 1 year ago

same issue, openSUSE Tumbleweed, s390x, Python 3.11

Dreamsorcerer commented 2 months ago

I've not seen this in the CI for some time. Anybody still reproducing this?