fastapi
fastapi copied to clipboard
fastapi dev runs stuck
Privileged issue
- [X] I'm @tiangolo or he asked me directly to create an issue here.
Issue Content
The first time I ran fastapi dev, there was no problem, but I encountered a code error. Then after I modified the code and saved it, a WARNING: WatchFiles detected changes in 'tool/index.py'. Reloading... then got stuck and could not exit.
Any updates on this @kingcwt? facing the same issue!
Could be related to: https://github.com/encode/uvicorn/issues/2000
I currently have a slightly different issue from yours @kingcwt but it's still an issue with fastapi cli (dev and run)
The Code
The Error
do not use the fastapi dev to run your application just use uvicorn instead, i do not face any issue with uvicorn
I tryied to reproduce it, but without success.
But sometimes I get the same problem running uvicorn with --reload in cmder/windows. I think it is related to uvicorn instead of fastapi
I'm having the same issue here on windows 10. I think this is related: https://www.reddit.com/r/FastAPI/comments/13oynf6/keeps_old_routes_after_changing_cache_problem/
It is like fastapi keeps an old version of the code cached somewhere. Even if I force kill fastpi and restart it, it loads an old version of my code.
It works (for a while) if I change port.
edit: this seems to be a uvicorn open issue which in turn is caused by a bug in cpython (open issue)
I think uvicorn dev server is better as of now. As we are not getting any updates about the Fast API cli. Its important to note that this may be fixed in near future. It happens a lot with Fast API server. So you have to kill the terminal and open a new one to re-run the project. Its painful.
@Sarmad426 Yes, you are right i am also facing the same issue I tried with both the commands such as fast API dev filename or if name == "main": uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True) Sometimes it stuck you have to forcefully close the server but still, the port is running on the cache. So you have to run the server again on a different port.
Running into a similar issue. I can I kill the the process with ctrl+c, but I cannot run on the same port. Attempting to run on the same port prints out the message like everything is working fine, but trying to access /docs spins forever. Switching to a new port works fine.
- Windows PowerShell (occurs both from Windows Terminal and PyCharm Terminal)
- Python 3.12
- Fast API 0.112.2
in pycharm if you use the "run" method to run the project then it tends to hand after reloading. if you task manager kill python it reloads again. something definately fishy there.
if you just do uvicorn --reload in terminal it tends to work better but still a massive PITA on windows :( easier to just not run it with reload
win11, py3.12, pycharm
can confirm. win 11. issue present at least month.
WARNING: WatchFiles detected changes in 'config.py'. Reloading...
and its stuck.
Its works fine until certain moment but hard to debug when exactly. after kill and start new it can stuck again.
No pycharm, vscode + .venv, server started by hand in terminal (❯ fastapi dev webserver.py --port 8080)
UPDATE:
do not use the fastapi dev to run your application just use uvicorn instead, i do not face any issue with uvicorn
relating to the comments, this doesnt look to help aswell.
I wonder if this would still happen with uvicorn's --timeout-graceful-shutdown
option. Using fastapi dev
, you have to set it using an environment variable UVICORN_TIMEOUT_GRACEFUL_SHUTDOWN=5
.
I have been getting this same problem whether uvicorn main:app --reload
or fastapi dev
method launching from powershell. (no pycharm here)
- Win11.
- VSCode
- Python 3.12.6
- FastAPI 0.115.0
- Uvicorn 0.31.0
Resorting to Task Manager (Ctrl+Shift+Esc) and killing python tasks as Ctrl+C often won't quit server and closing powershell window and launching another doesn't seem to kill it either.
Installed fastapi using "fastapi[standard]"
and also removed and tried just installing separately fastapi
and uvicorn
Based on this thread #2000 just to test tried downgrading just uvicorn to 0.22 with no difference then to 0.21 (0.21.1) and sure enough it stops being stuck and reloads in a second?
WARNING: StatReload detected changes in 'api.py'. Reloading...
INFO: Started server process [9192]
INFO: Waiting for application startup.
INFO: Application startup complete.
I am also facing this issue recently with FastAPI reloading. When we start the server, it works fine, and the APIs are functioning correctly. However, when we make changes to the code and save it, the FastAPI server does not respond when the API is hit again, and the FastAPI server page shows the reload icon for a long time.
Windows stuff, I see, using WSL2 never encountered this issue.
For me the problem was avast scanning the app before allowing it to execute thus taking a long time. Disabled avast and suddenly it was working well
I am facing a similar issue using Uvicorn. When I make changes to the code and save it, Uvicorn reloads, showing the message 'Application shutdown completed.' After that, the APIs stop responding, and the terminal becomes unresponsive. Even pressing Ctrl+C doesn't work
I had the same problem with PyCharm FastAPI. When introducing a change, it used to get stuck on reload. (most likely couldn't kill the current process.) But I could resolve it by just using the terminal and running
uvicorn main:app --reload
It then worked like a charm.
I noticed that no matter what I do in Run/Debug Configuration of PyCharm, it wont be fixed there. I.E. if I choose Uvicorn in Run/Debug Config, I would still have the same problem. For me it works only when I lunch the server from terminal using Uvicorn.
I had same problem, fastapi dev
got stuck. Then after i killed the terminal and started again, the schemas in the docs got stucks, it won't be updated even i modify the model schemas, it will works again when you start dev server with different port.