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

Slow reload on Windows 10 using PyCharm #2000

Open
Kludex opened this issue Jun 8, 2023 Discussed in #1977 · 14 comments
Open

Slow reload on Windows 10 using PyCharm #2000

Kludex opened this issue Jun 8, 2023 Discussed in #1977 · 14 comments
Labels

Comments

@Kludex
Copy link
Member

Kludex commented Jun 8, 2023

Discussed in #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
@3to1null
Copy link

3to1null commented Jun 8, 2023

I think issue lies somewhere here:

def restart(self) -> None:
if sys.platform == "win32": # pragma: py-not-win32
self.is_restarting = True
assert self.process.pid is not None
os.kill(self.process.pid, signal.CTRL_C_EVENT)
else: # pragma: py-win32
self.process.terminate()
self.process.join()
self.process = get_subprocess(
config=self.config, target=self.target, sockets=self.sockets
)
self.process.start()

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
Copy link

3to1null commented Jun 8, 2023

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

@Kludex
Copy link
Member Author

Kludex commented Jun 8, 2023

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

@3to1null
Copy link

3to1null commented Jun 8, 2023

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

@rcoor
Copy link

rcoor commented Jul 5, 2023

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.

@altq33
Copy link

altq33 commented Jul 11, 2023

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

@Kludex
Copy link
Member Author

Kludex commented Jul 11, 2023

Is there someone having this issue without pycharm?

@altq33
Copy link

altq33 commented Jul 11, 2023

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

@Kludex
Copy link
Member Author

Kludex commented Jul 11, 2023

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)

@encode encode locked as spam and limited conversation to collaborators Jul 11, 2023
@Kludex Kludex changed the title v0.22.0 on Windows 10 took minutes to reload Slow reload on Windows 10 using PyCharm Jul 11, 2023
@encode encode unlocked this conversation Jul 30, 2023
@encode encode locked as spam and limited conversation to collaborators Jul 30, 2023
@encode encode unlocked this conversation Oct 2, 2023
@Kludex
Copy link
Member Author

Kludex commented Oct 2, 2023

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

@leonardomarcao
Copy link

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

@3to1null
Copy link

@soulee-dev
Copy link

Still struggling same issue.
Should be changed...

@encode encode locked as spam and limited conversation to collaborators Mar 30, 2024
@Kludex
Copy link
Member Author

Kludex commented Mar 30, 2024

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

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

No branches or pull requests

6 participants