Fastapi: [BUG] Error with the reloader

Created on 3 Dec 2019  Â·  5Comments  Â·  Source: tiangolo/fastapi

Describe the bug

Whenever I do

uvicorn api.index:app --reload

Then change the file index, I get an error like this

Accept failed on a socket
socket: <asyncio.TransportSocket fd=592, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 8000)>
(See the full log below)

To Reproduce

  1. Create a file with:
# api/index.py
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def index():
    print("boo")
    return {"Hello": "World"}
  1. run the app using uvicorn api.index:app --reload.
  2. Open the browser and call the endpoint /.
  3. Change the content of the file api/index.py so that the reloader gets triggered.
  4. I expected it to reload the server, instead the error specified below happened.

Expected behavior

Reload the application, and continue to work as normal.

Environment

  • OS: Windows
  • FastAPI Version [e.g. 0.3.0]:
python -c "import fastapi; print(fastapi.__version__)"
email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
0.44.0
  • Python version:
python --version
Python 3.8.0

Logs

uvicorn index:app --reload
?[32mINFO?[0m: Uvicorn running on ?[1mhttp://127.0.0.1:8000?[0m (Press CTRL+C to quit)
?[32mINFO?[0m: Started reloader process [?[36m?[1m1232?[0m]
?[33mWARNING?[0m: email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
?[32mINFO?[0m: Started server process [?[36m13256?[0m]
?[32mINFO?[0m: Waiting for application startup.
?[32mINFO?[0m: Application startup complete.
?[33mWARNING?[0m: Detected file change in 'index.py'. Reloading...
?[33mWARNING?[0m: email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
?[32mINFO?[0m: Started server process [?[36m1064?[0m]
?[31mERROR?[0m: Accept failed on a socket
socket: Traceback (most recent call last):
File "C:\Users\wass1\AppData\Local\Programs\Python\Python38\Lib\asyncio\proactor_events.py", line 817, in loop
f = self._proactor.accept(sock)
File "C:\Users\wass1\AppData\Local\Programs\Python\Python38\Lib\asyncio\windows_events.py", line 545, in accept
self._register_with_iocp(listener)
File "C:\Users\wass1\AppData\Local\Programs\Python\Python38\Lib\asyncio\windows_events.py", line 714, in _register_with_iocp
_overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect
?[32mINFO?[0m: Waiting for application startup.
?[32mINFO?[0m: Application startup complete.

bug

All 5 comments

I believe this is an issue with uvicorn on Windows. I think if you use the asyncio event loop the issue goes away. I can’t easily look for them now but I recall one or two recent issues where this was raised and solved. @euri10 might know more?

Yep it's https://github.com/encode/uvicorn/issues/509

Le mer. 4 déc. 2019 à 3:35 AM, dmontagu notifications@github.com a écrit :

I believe this is an issue with uvicorn on Windows. I think if you use the
asyncio event loop the issue goes away. I can’t easily look for them now
but I recall one or two recent issues where this was raised and solved.
@euri10 https://github.com/euri10 might know more?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/767?email_source=notifications&email_token=AAINSPSJHROB5UMSXWMFA3TQW4JQBA5CNFSM4JU7VFM2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEF3QOWQ#issuecomment-561448794,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAINSPVW53S7ZTH2MTAZSALQW4JQBANCNFSM4JU7VFMQ
.

I encountered this issue too. I turned to use docker and it won't bother. Here's what I found, hope it helps:

use tiangolo/uvicorn-gunicorn and use CMD /start-reload.sh in Dockerfile;

set volume in the docker-compose.yml

volumes:
 - .:/app

It seems this should be solved in recent versions of Uvicorn.

Did you solve your problem @WassimBenzarti ?

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings