Skip to content

Flush the stdout buffer on Windows to trigger reload #2604

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

Merged
merged 5 commits into from
Apr 14, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions uvicorn/supervisors/basereload.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def restart(self) -> None:
self.is_restarting = True
assert self.process.pid is not None
os.kill(self.process.pid, signal.CTRL_C_EVENT)
print("") # This helps trigger windows to respond to the Ctrl+C event
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to use sys.stdout.flush() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I did give this a go before and sys.stdout.flush() on its own doesn't seem to be enough. Based on Copilot's review, I also tried the following:

  • sys.stdout.write("") then sys.stdout.flush() - No success here, it still doesn't trigger a reload.
  • sys.stdout.write("\n") then sys.stdout.flush() - This is closer to print("") and does work. But at that point, I'm not sure it's any better than just using print("")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like sys.stdout.write(" ") then sys.stdout.flush() works too actually. That's a little less intrusive to the logs than printing a blank line and possibly a little bit clearer at showing what's required to get a reload to happen (something needs to be printed & the buffer need to be flushed)

else: # pragma: py-win32
self.process.terminate()
self.process.join()
Expand Down