-
-
Notifications
You must be signed in to change notification settings - Fork 844
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
Windows - Python 3.8+ raises "RuntimeError: Event Loop is closed" on exit. #914
Comments
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? |
Also: May well be specific to Window's Proactor event loop. |
Yes, definitely looks like a Windows specific issue to me - the linked issues are for Windows too. |
@florimondmanca , yes upgraded to 3.8.2 to check, the result is the same. |
Can confirm this is an issue with the latest PyPi version of aiohttp and Python 3.8.2 on Windows. |
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? |
It'd also be helpful to try switching to using Python only switched the default event loop to proactor from 3.8 onwards. https://docs.python.org/3/library/asyncio-policy.html#asyncio.DefaultEventLoopPolicy |
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 <[email protected]> a
écrit :
… It'd also be helpful to try switching to using SelectorEventLoop and 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
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#914 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAINSPTJI3OZEDU6VKCF2KLRPKBHTANCNFSM4MVRF2GQ>
.
|
The workaround is to use the selector event loop like this: import sys
import asyncio
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) The following from the documentation will not resolve this issue (for some reason): import asyncio
import selectors
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop) |
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 |
Can anyone on Windows, running Python 3.8+ confirm what currently happens when running this?... import httpx
import asyncio
async def main():
async with httpx.AsyncClient() as client:
r = await client.get("https://login.microsoftonline.com/")
print(r)
asyncio.run(main(), debug=True) Trying to verify if we're still exposed to this issue. (I guess we are but 🤷♂️) |
Using:
I have a |
@dalf - Thanks! Also uh... that's not at all what I was expecting / looking for. 🙃🙃🙃 Does that replicate on other URLs?...
Does removing the Does it work okay in the sync version?... import httpx
def main():
with httpx.Client() as client:
r = client.get("https://login.microsoftonline.com/")
print(r)
main() Or with trio?... (Use import httpx
import trio
async def main():
async with httpx.AsyncClient() as client:
r = await client.get("https://login.microsoftonline.com/")
print(r)
trio.run(main) |
Maybe my report is irrelevant, I'm not familiar with the Python Windows environment:
|
🤨 That's pretty relevant. But just super surprising. Are you able to double double check you've got fully up to date httpx and httpcore versions installed? |
To do the test, I've installed Python from https://www.python.org/ftp/python/3.8.5/python-3.8.5.exe (it wasn't installed before), and just pip freeze shows
I think a test from someone else would be helpful. |
I ran some tests: Test against
|
Windows 10 Code gets the required data from BSCSCAN and then i get the Runtime Error. PYTHON YOUR_API_KEY = "REDACTED" async def main(): if name == "main": |
I'm having the same issue on Windows 10. It does not matter what library I'm using. |
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?... import asyncio
import httpx
async def main():
async with httpx.Client() as client:
r = await client.get("https://www.example.com")
print(r)
if name == "main":
asyncio.run(main()) Also, does it replicate with both |
Nope, it works pretty well for me. I get
|
@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. |
when using |
This bug in the Python 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? |
I can test later today. |
This problem also occurs on linux.
|
Have you considered switching to recent version of python? |
Спорадическая ошибка. Не уверен, что лечение помогает, но попробовать нужно. Инструкция здесь: encode/httpx#914 (comment)
Checklist
master
.Describe the bug
I get the following traceback when running the attached code
To reproduce
Inserting
time.sleep(0.1)
at the end ofmain()
fixes it. Possibly due to the same reasons in the issues linked below.Environment
Possibly the same as this and this
The text was updated successfully, but these errors were encountered: