Slow reload on Windows 10 using PyCharm
Discussed in https://github.com/encode/uvicorn/discussions/1977
Originally posted by adhihargo May 14, 2023 Uvicorn used to work fine in my dev laptop running Windows 10, but now every time there's a modified change, it stuck in this line:
WARNING: WatchFiles detected changes in 'main.py'. Reloading...
... for nearly 5 minutes before actually reloading the files (printing Shutting down in the next line, etc.). During that time, the API server part works normally but serving the older version before file modification..
Downgraded to v0.21.1, and reloading works instantly again.
[!IMPORTANT]
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
I think issue lies somewhere here: https://github.com/encode/uvicorn/blob/7d4eab20ac0cf0fb07b3afaa9ca35a2ff09fb499/uvicorn/supervisors/basereload.py#L87-L99
With the changes introduced in #1584, it will hang for multiple minutes on the process.join() call. If I remove the changes, so the function looks like:
def restart(self) -> None:
self.process.terminate()
self.process.join()
self.process = get_subprocess(
config=self.config, target=self.target, sockets=self.sockets
)
self.process.start()
The reload is pretty much instant, like it was on earlier versions.
I can only reproduce this issue when the program is run using Pycharm. If I run it directly (either via uvicorn.run("app:app", reload=True) in Python, or with the uvicorn app:app --reload from the terminal), this issue is not present.
While it would be very nice to have this working again, I am not sure if this is something that should be fixed in Uvicorn (if this is even possible).
Edit: By enabling 'emulate terminal in output console' in Pycharm's edit run configuration dialog, the reload works as expected. However, this also seems to disable ANSI escape sequences, so the output logs look quite bad.
Got it.
Please open an issue on PyCharm's issue tracker. If they tell me it's a Uvicorn bug, and explain why, we can think about the next step. 👍
Done: https://youtrack.jetbrains.com/issue/PY-60962/os.killpid-signal.CRTLCEVENT-ignored-when-running-python-program-from-Pycharm.
I've added a very small program that has the same issue and does not use Uvicorn. I do not think this is a Uvicorn bug, however if Jetbrains does not want to fix this, in my opinion it might still be worth it to investigate if we can support this in Uvicorn by not using os.kill(self.process.pid, signal.CTRL_C_EVENT). Having a working reloader and being able to use Pycharm is probably the expected behaviour for a lot of developers.
btw, thank you for the quick responses :)
Hello, I am getting a similar error on 0.22.0 but it happens both when I run with uvicorn.run or in the terminal, with --reload.
The same problem right now on FastAPI and default pycharm run settings with --reload
Is there someone having this issue without pycharm?
Is there someone having this issue without pycharm?
No, this problem occurs when starting the server through pycharm tools, if you run the server with the same command through the terminal, --reload works instantly
Thanks @altq33 :)
I'm locking this issue since people keep spamming here.
This is a pycharm issue: youtrack.jetbrains.com/issue/PY-60962/os.killpid-signal.CRTLCEVENT-ignored-when-running-python-program-from-Pycharm.
If you have an issue with Windows (not using Pycharm), please create a new issue.
(I'll not close this issue so people can be aware)
I've unlocked the issue, so people can concentrate the complains here instead of creating multiple issues/discussions.
@Kludex @altq33 @3to1null any resolution? I have the same problem... :'( I having troubleshoot using the Uvicorn an FasAPI in debugger mode.
Some information about my environment:
- Poetry venv
- Python 3.12
- last versions of FastAPI, uvicorn etc.
@leonardomarcao The issue in Pycharm still persists and is tracked here: https://youtrack.jetbrains.com/issue/PY-60962/os.killpid-signal.CRTLCEVENT-ignored-when-running-python-program-from-Pycharm
Still struggling same issue. Should be changed...
Locking this issue again. This is a PyCharm issue, not Uvicorn.
Here: https://youtrack.jetbrains.com/issue/PY-60962/os.killpid-signal.CRTLCEVENT-ignored-when-running-python-program-from-Pycharm