You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Windows - Python 3.8+ raises "RuntimeError: Event Loop is closed" on exit. #914
[ x] The bug is reproducible against the latest release and/or master.
[ x] There are no similar issues or pull requests to fix it yet.
Describe the bug
I get the following traceback when running the attached code
404
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000017C63D5E310>
Traceback (most recent call last):
File "C:\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
self.close()
File "C:\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Python\Python38\lib\asyncio\base_events.py", line 715, in call_soon
self._check_closed()
File "C:\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
I assume this is yet another strange issue with asyncio’s stream.close() - it schedules the closing of the socket but it’s not guaranteed to be done once client.aclose finishes, which means the socket is actually deleted upon garbage collection, and by then I suppose the event loop is already closed... The sleep() call fixed this because it allows asyncio to execute the close callback before proceeding to shutdown the async environment.
See also #825 for more discussion on a possibly related issue.
FYI this is likely to be something to address on the HTTPCore side, as connection management and networking is done there. Might then be helpful to first come up with a minimal repro example using HTTPCore only?
not sure there's a link and can't remember why now but in uvicorn we force
the SelectorEventLoop to be the default for windows
Le ven. 1 mai 2020 à 10:23 AM, Tom Christie <notifications@github.com> a
écrit :
Reacted by Matt Sullivan, Abhinav Kasamsetty, Andrii, 二刺螈, Yevhenii Kovalchuk, BSON, Joshua Arnott, narigama, Andriy Stolyar, zhengjianyang and 37 more
Reacted by Rami Shareef
Reacted by Karl, Jacopo, BSON, narigama, Aineah Simiyu, John Just, Anton Berdnikov, Robert Newton-Nomura, Lena, Sergey Pavlov and 19 more
Same as with #825 - Haven't looked into how awkward it is to do, but really I think we'd want to monkeypatch asyncio's socket unwrapping behaviour, so that we can reliably call .wait_closed(...) without it potentially hanging.
I'm having the same issue on Windows 10. It does not matter what library I'm using.
If the loop is an ProactorEventLoop, then it is guaranteed that it will cause an exception if
there have been any network requests made which use SSL or sessions.
when using async with httpx.AsyncClient() as client the transport should be closed correctly by anyio and not trigger the __del__ so this looks like an anyio bug
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x7f85c758ce50>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/base_subprocess.py", line 126, in __del__
self.close()
File "/usr/lib/python3.8/asyncio/base_subprocess.py", line 104, in close
proto.pipe.close()
File "/usr/lib/python3.8/asyncio/unix_events.py", line 536, in close
self._close(None)
File "/usr/lib/python3.8/asyncio/unix_events.py", line 560, in _close
self._loop.call_soon(self._call_connection_lost, exc)
File "/usr/lib/python3.8/asyncio/base_events.py", line 711, in call_soon
self._check_closed()
File "/usr/lib/python3.8/asyncio/base_events.py", line 504, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Reacted by Ananda Kumar, Benjamin Levin, Kaloyan Beshev, Jonas Süskind, hien.pt, floby, Andrei Gasparian, Harshal Sheth, Davide Gessa, Minglei Li and 1 more
ERROR Task exception was never retrieved
future: <Task finished name='Task-288' coro=<AsyncClient.aclose() done, defined at \.venv\Lib\site-packages\httpx\_client.py:1978> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
File "\.venv\Lib\site-packages\httpx\_client.py", line 1985, in aclose
await self._transport.aclose()
File "\.venv\Lib\site-packages\httpx\_transports\default.py", line 406, in aclose
await self._pool.aclose()
File "\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 353, in aclose
await self._close_connections(closing_connections)
File "\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 345, in _close_connections
await connection.aclose()
File "\.venv\Lib\site-packages\httpcore\_async\connection.py", line 173, in aclose
await self._connection.aclose()
File "\.venv\Lib\site-packages\httpcore\_async\http11.py", line 258, in aclose
await self._network_stream.aclose()
File "\.venv\Lib\site-packages\httpcore\_backends\anyio.py", line 53, in aclose
await self._stream.aclose()
File "\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 1314, in aclose
self._transport.close()
File "C:\Users\<user>\AppData\Roaming\uv\python\cpython-3.12.10-windows-x86_64-none\Lib\asyncio\proactor_events.py", line 109, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\<user>\AppData\Roaming\uv\python\cpython-3.12.10-windows-x86_64-none\Lib\asyncio\base_events.py", line 799, in call_soon
self._check_closed()
File "C:\Users\<user>\AppData\Roaming\uv\python\cpython-3.12.10-windows-x86_64-none\Lib\asyncio\base_events.py", line 545, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Activity
florimondmanca commentedon Apr 30, 2020
I assume this is yet another strange issue with asyncio’s stream.close() - it schedules the closing of the socket but it’s not guaranteed to be done once client.aclose finishes, which means the socket is actually deleted upon garbage collection, and by then I suppose the event loop is already closed... The sleep() call fixed this because it allows asyncio to execute the close callback before proceeding to shutdown the async environment.
See also #825 for more discussion on a possibly related issue.
Have you tried running this on Python 3.8.2?
lovelydinosaur commentedon Apr 30, 2020
Also: May well be specific to Window's Proactor event loop.
florimondmanca commentedon Apr 30, 2020
Yes, definitely looks like a Windows specific issue to me - the linked issues are for Windows too.
rmawatson commentedon Apr 30, 2020
@florimondmanca , yes upgraded to 3.8.2 to check, the result is the same.
Baysul commentedon May 1, 2020
Can confirm this is an issue with the latest PyPi version of aiohttp and Python 3.8.2 on Windows.
florimondmanca commentedon May 1, 2020
Okay, thank you.
I personally don’t have a Windows machine so help with investigation and possible fixes would help.
Would we need to consider black magic such as aio-libs/aiohttp#1925 (comment)? How about aio-libs/aiohttp#3733?
FYI this is likely to be something to address on the HTTPCore side, as connection management and networking is done there. Might then be helpful to first come up with a minimal repro example using HTTPCore only?
lovelydinosaur commentedon May 1, 2020
It'd also be helpful to try switching to using
SelectorEventLoopand let us know what the behaviour is like then... https://docs.python.org/3/library/asyncio-eventloop.html#event-loop-implementations (Or to try with Python 3.7 or earlier)Python only switched the default event loop to proactor from 3.8 onwards. https://docs.python.org/3/library/asyncio-policy.html#asyncio.DefaultEventLoopPolicy
euri10 commentedon May 1, 2020
Baysul commentedon May 1, 2020
The workaround is to use the selector event loop like this:
The following from the documentation will not resolve this issue (for some reason):
rmawatson commentedon May 2, 2020
Thanks @Baysul.
This bug report looks like it is tracking the same issue.
lovelydinosaur commentedon May 6, 2020
Same as with #825 - Haven't looked into how awkward it is to do, but really I think we'd want to monkeypatch asyncio's socket unwrapping behaviour, so that we can reliably call
.wait_closed(...)without it potentially hanging.lovelydinosaur commentedon Aug 20, 2020
Can anyone on Windows, running Python 3.8+ confirm what currently happens when running this?...
Trying to verify if we're still exposed to this issue. (I guess we are but 🤷♂️)
21 remaining items
pprovart commentedon Feb 10, 2022
Windows 10
Python 3.10.2
NOTE: Problem occured before and after i installed httpx and httpcore
httpcore 0.14.7
httpx 0.22.0
Code gets the required data from BSCSCAN and then i get the Runtime Error.
The time.sleep(1) command didnt work.
PYTHON
import asyncio
from bscscan import BscScan
YOUR_API_KEY = "REDACTED"
CON_ADDR = "0xa38898a4ae982cb0131104a6746f77fa0da57aaa"
WAL_ADDR = "0x411709fd0cdd5f06ff6ef45b48aa0cb9842f01fc"
async def main():
async with BscScan(YOUR_API_KEY) as bsc:
print(await bsc.get_bnb_balance(address="0xa38898a4ae982cb0131104a6746f77fa0da57aaa")
if name == "main":
asyncio.run(main())
OUTPUT

aabmets commentedon Feb 24, 2022
I'm having the same issue on Windows 10. It does not matter what library I'm using.
If the loop is an ProactorEventLoop, then it is guaranteed that it will cause an exception if
there have been any network requests made which use SSL or sessions.
lovelydinosaur commentedon Feb 25, 2022
Okay - could you show me that absolute simplest possible example that reproduces this.
@pprovart's example here is pretty good - does it still replicate if you simplify it even further?...
Also, does it replicate with both
httpandhttpsURLs?lovelydinosaur commentedon Feb 25, 2022
@Mennaruuk - Ah yup. Just looked though this thread and reminded myself of the comment here.
This is a bug in Python's stdlib with Windows. See: https://bugs.python.org/issue39232
One option here might be to check if the ProactorEventLoop is the default, and warn if it is.
graingert commentedon Jun 18, 2022
when using
async with httpx.AsyncClient() as clientthe transport should be closed correctly by anyio and not trigger the__del__so this looks like an anyio buggraingert commentedon Jun 18, 2022
@pprovart is using aiohttp and not httpx https://github.com/pcko1/bscscan-python/blob/fcfc30cb7d1a4ff2a4f2de30606f35698675cbc7/bscscan/core/async_client.py#L32
lovelydinosaur commentedon Dec 11, 2022
This bug in the Python
stdlibappears to have been fixed now in 3.11 (and possibly backported to 3.10?)python/cpython#83413
Are any windows users here able to attempt replicating the original issue on the latest patch versions of Python 3.9, 3.10 and 3.12?
mbwmbw1337 commentedon Dec 11, 2022
I can test later today.
FANGOD commentedon Jan 18, 2023
This problem also occurs on linux.
kpostekk commentedon Jan 18, 2023
Have you considered switching to recent version of python?
fix(outlook): Попытка исправить ошибку «Asyncio Event Loop is Closed»
piscesvivian commentedon Aug 1, 2025
V-0.28.1 Windows The error is still there