uvicorn icon indicating copy to clipboard operation
uvicorn copied to clipboard

Slow reload on Windows 10 using PyCharm

Open Kludex opened this issue 2 years ago • 14 comments

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.
Fund with Polar

Kludex avatar Jun 08 '23 12:06 Kludex

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.

3to1null avatar Jun 08 '23 13:06 3to1null

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. image

3to1null avatar Jun 08 '23 13:06 3to1null

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. 👍

Kludex avatar Jun 08 '23 14:06 Kludex

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 :)

3to1null avatar Jun 08 '23 14:06 3to1null

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.

rcoor avatar Jul 05 '23 06:07 rcoor

The same problem right now on FastAPI and default pycharm run settings with --reload

altq33 avatar Jul 11 '23 08:07 altq33

Is there someone having this issue without pycharm?

Kludex avatar Jul 11 '23 09:07 Kludex

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

altq33 avatar Jul 11 '23 10:07 altq33

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)

Kludex avatar Jul 11 '23 10:07 Kludex

I've unlocked the issue, so people can concentrate the complains here instead of creating multiple issues/discussions.

Kludex avatar Oct 02 '23 04:10 Kludex

@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 avatar Mar 20 '24 22:03 leonardomarcao

@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

3to1null avatar Mar 21 '24 11:03 3to1null

Still struggling same issue. Should be changed...

soulee-dev avatar Mar 30 '24 08:03 soulee-dev

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

Kludex avatar Mar 30 '24 08:03 Kludex