Skip to content
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

OSError occurs when workers > 1 #1317

Closed
2 tasks done
mnghiap opened this issue Jan 10, 2022 · 4 comments · Fixed by #1454
Closed
2 tasks done

OSError occurs when workers > 1 #1317

mnghiap opened this issue Jan 10, 2022 · 4 comments · Fixed by #1454

Comments

@mnghiap
Copy link

mnghiap commented Jan 10, 2022

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

This bug outputs an error similar to #529 but occurs on different parameters of uvicorn. It occurs when I run uvicorn with the following settings:
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=2, reload=False, loop="asyncio")
I received the following exceptions:

INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started parent process [16448]
INFO: Started server process [17848]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [8104]
INFO: Waiting for application startup.
INFO: Application startup complete.
Accept failed on a socket
socket: <asyncio.TransportSocket fd=3552, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 8000)>
Traceback (most recent call last):
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 834, in loop
f = self._proactor.accept(sock)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 549, in accept
self._register_with_iocp(listener)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 718, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect

Note that the app is still working completely fine. Setting either workers=1 or reload=True makes the exceptions go away, while the app still works perfectly fine. Setting workers=n to any number n>1 makes the exception appear n-1 times. For example, with uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=4, reload=False, loop="asyncio"):

INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started parent process [14188]
INFO: Started server process [18744]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [25288]
INFO: Waiting for application startup.
INFO: Application startup complete.
Accept failed on a socket
socket: <asyncio.TransportSocket fd=880, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 8000)>
Traceback (most recent call last):
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 834, in loop
f = self._proactor.accept(sock)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 549, in accept
self._register_with_iocp(listener)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 718, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect
INFO: Started server process [25760]
INFO: Waiting for application startup.
INFO: Application startup complete.
Accept failed on a socket
socket: <asyncio.TransportSocket fd=1772, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 8000)>
Traceback (most recent call last):
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 834, in loop
f = self._proactor.accept(sock)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 549, in accept
self._register_with_iocp(listener)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 718, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect
INFO: Started server process [21280]
INFO: Waiting for application startup.
INFO: Application startup complete.
Accept failed on a socket
socket: <asyncio.TransportSocket fd=3872, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 8000)>
Traceback (most recent call last):
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 834, in loop
f = self._proactor.accept(sock)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 549, in accept
self._register_with_iocp(listener)
File "C:\Users\phanm\AppData\Local\Programs\Python\Python38\lib\asyncio\windows_events.py", line 718, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect

Steps to reproduce the bug

No response

Expected behavior

No response

Actual behavior

No response

Debugging material

No response

Environment

  • OS: Windows-10-10.0.19042-SP0
  • Python: 3.8.10
  • uvicorn: 0.16.0
  • uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=2, reload=False, loop="asyncio")

Additional context

The exceptions do not occur with the following settings:
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=2, reload=True, loop="asyncio")
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=1, reload=False, loop="asyncio")
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=1, reload=True, loop="asyncio")

@grahamrjuk
Copy link

grahamrjuk commented Jan 14, 2022

I have this problem as well - reload=True with workers >= 2 removes the error.
reload=False causes the error.

Presumably it works with reload=True since that ignores the workers request and uses only one.

I see a log message:
asyncio proactor_events DEBUG Using proactor: IocpProactor

Windows 10 Pro
Python 3.9.4
Uvicorn 0.16.0

This does not happen with Uvicorn 0.15.0
The equivalent log message is:
asyncio selector_events DEBUG Using selector: SelectSelector

@euri10
Copy link
Member

euri10 commented Feb 17, 2022

can confirm I boote a win box, the app seems functional though just with this error message
note that running from the CLI you dont have the issue, and it seems tied to we using iocp again
PR welcome if you find the root of it !

@euri10
Copy link
Member

euri10 commented Feb 17, 2022

maybe the issue is that in the multiprocess case we dont call Server.startup but Server.serve directly, and iirc in startup method we have something to share sockets on windows so maybe we got the flow wrong here ?

@smitchell6879
Copy link

This seemed to fix this issue for me Pull Request #1443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants