-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
"RuntimeError: Event loop is closed" when ProactorEventLoop is used #4324
Comments
The aiohttp master will fix this. Sorry, the change cannot be backported to 3.x branch. Big enough timeout may saturate the problem. Also, you can ignore all errors on the loop closing as an option. That's sad that asyncio proactor raises an exception instead of an expected warning. |
why is this code giving |
This is not s solution, because |
Yes I did not read the topic clearly, I am sorry. |
in |
Running EDIT: Peraphs there is because |
EDIT (after almost 2 years of fighting with more and more aiohttp bugs)**As another solution in another issue that is related and more to the root cause, use aiohttp 4.0 or use this code when closing client session #1925, monkey patching is not recommended if another solution exists. Old monkey patchingI found another solution for this problem if some still having issues with it. This involves directly manipulating the class method itself, it might be dirty in some peoples eyes. This applies to situations where you need to close your loops after a tasks like web apps and other similar situations. from functools import wraps
from asyncio.proactor_events import _ProactorBasePipeTransport
def silence_event_loop_closed(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except RuntimeError as e:
if str(e) != 'Event loop is closed':
raise
return wrapper
_ProactorBasePipeTransport.__del__ = silence_event_loop_closed(_ProactorBasePipeTransport.__del__) EDIT: You may want to do this only on Windows environment to avoid executing it in production (although if platform.system() == 'Windows':
# Silence the exception here.
_ProactorBasePipeTransport.__del__ = silence_event_loop_closed(_ProactorBasePipeTransport.__del__) |
@paaksing Thank you for that. Those errors were bugging me. |
@paaksing the code worked like gold. I have been scratching my hair off for days trying to fix this on my discord bot and then I saw this |
in: <function _ProactorBasePipeTransport.__del__ at. The workaround comes from aio-libs/aiohttp#4324 (comment)
# aio-libs/aiohttp#4324 Add get_vlan methods Tidy Up, Black formatting
I don't understand. Why is this still not fixed even though this issue has been closed for almost 1.5 years? Can't you fix it? Can't you be arsed to fix it? What's going on? |
@paaksing I see, thanks. You're wrong about this being fixed in python 3.9 though. Guess we'll wait for python 3.10? |
@Aran-Fey It's fixed for me on 3.9 tho, I honestly don't know the spaghetti behind this, but this is windows specific issue aswell. The workaround that I posted above should solve your issue. |
Also, refer to the comment that closed it and try not to be so rude: #4324 (comment) |
@Dreamsorcerer I've seen that comment. It contains no useful information. "This will be fixed in master"? Clearly not. What am I supposed to take from that comment? There's nothing there except maybe the "neither of us uses asyncio on Windows" bit, which doesn't really mean anything either, but kinda sounds like "We don't know how to fix it". Also, when was I rude? |
Are you reproducing the error on master? If so, then perhaps the issue needs to be reopened, or a new issue made.
In the comment I quoted, and your messaging still sounds aggressive... |
@Aran-Fey
Also I am not forcing you to include that in your project, you asked for a fix, I gave you a "fix that you said it is not", you still got the golden finger to decide what to do with your project. |
Ah, yes, I get a similar harmless RuntimeError, the message I'm ignoring is "Event loop stopped before Future completed." |
Feel free to create a new issue with some code which reproduces it. It may just be something you overlooked in your code. |
This change is a Windows OS-specific HACK to silence an exception thrown on the asyncio event loop being closed as part of the asyncio library's proactor. Note that the exception is technically harmless to the library's functionality, but it is a poor user experience to see exceptions with normal usage. Hack sourced from an issue on the aiohttp library: aio-libs/aiohttp#4324 (comment)
* v2.6.1 Fix uncaught exception on Windows This change is a Windows OS-specific HACK to silence an exception thrown on the asyncio event loop being closed as part of the asyncio library's proactor. Note that the exception is technically harmless to the library's functionality, but it is a poor user experience to see exceptions with normal usage. Hack sourced from an issue on the aiohttp library: aio-libs/aiohttp#4324 (comment) * Bump version
There seems to be an issue with asyncio/aiohttp on windows. Using asyncio.run(coro()) results in a RuntimeError. From the traceback, it looks like it is comming from asyncio. More info aio-libs/aiohttp#4324 and https://stackoverflow.com/questions/59255332/exception-event-loop-is-closed-with-aiohttp-and-asyncio-in-python-3-8
For more information look at aio-libs/aiohttp#4324
I don't think any of the above posted workarounds are ideal, either silencing the exception (not desirable), or setting Better workaround might be :
or
|
This is apparently fixed on aiohttp 4.0 beta. A more serious issue is involved here, a solid workaround was provided in this comment: #1925 (comment) |
how to fix connectionreseterror: [winerror 10054] an existing connection was forcibly closed by the remote host |
|
Summary: Although exit code is 0, on Windows it finishes with exeption on tearing down of event loop. Some links: - https://bugs.python.org/issue39232 - aio-libs/aiohttp#4324 With `asyncio.get_event_loop().run_until_complete()` it works fine. Reviewed By: zertosh Differential Revision: D49373237 fbshipit-source-id: 1f64d6d42c67ce533395b29f19af1b9a085a918e
Summary: Although exit code is 0, on Windows it finishes with exeption on tearing down of event loop. Some links: - https://bugs.python.org/issue39232 - aio-libs/aiohttp#4324 With `asyncio.get_event_loop().run_until_complete()` it works fine. Reviewed By: zertosh Differential Revision: D49373237 fbshipit-source-id: 1f64d6d42c67ce533395b29f19af1b9a085a918e
Summary: Although exit code is 0, on Windows it finishes with exeption on tearing down of event loop. Some links: - https://bugs.python.org/issue39232 - aio-libs/aiohttp#4324 With `asyncio.get_event_loop().run_until_complete()` it works fine. Reviewed By: zertosh Differential Revision: D49373237 fbshipit-source-id: 1f64d6d42c67ce533395b29f19af1b9a085a918e
Worked for me with python3.11, but the code is as follows: import asyncio
origin_shutdown = asyncio.BaseEventLoop.shutdown_default_executor
async def _shutdown(self):
await origin_shutdown(self)
await asyncio.sleep(5)
asyncio.BaseEventLoop.shutdown_default_executor = _shutdown # type:ignore |
Long story short
When
ProactorEventLoop
is used, the lack of graceful shutdown in aiohttp results inRuntimeError
. (For comparison, whenSelectorEventLoop
is used, it results inResourceWarning
.) Note thatProactorEventLoop
is the default event loop on Windows in Python starting from 3.8.Maybe the root cause of this issue is the same as of #1925. But the impact is higher (
RuntimeError
instead ofResourceWarning
).Steps to reproduce
Actual behaviour
Expected behaviour
No exceptions. Maybe warnings, but no exceptions.
Your environment
aiohttp (client) version: 3.6.2
Python version: 3.7, 3.8
OS: Windows 10
Additional information
I do not know any workaround.
A delay before closing the event loop suggested in the documentation does not help in a bit more complicated example (try to run it several times or add more URLs to the list):
The text was updated successfully, but these errors were encountered: