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

Flush the stdout buffer on Windows to trigger reload #2604

Merged
merged 5 commits into from
Apr 14, 2025

Conversation

jamesh1999
Copy link
Contributor

@jamesh1999 jamesh1999 commented Mar 28, 2025

Summary

I'm raising this in response to #2000 as it's a fix I've been using locally. Through my experimentation, I have found that printing something after the Ctrl+C event is sent, allows the terminal running uvicorn to process the event, causing it to reload immediately.

I explain this in #2000 and there is already a reply saying it has resolved the issue for somebody else.

Tested on Windows 11 with:

  • PyCharm (built-in terminal)
  • VS Code (built-in terminal)
  • Windows Terminal app

All of these now reliably reload for me. However I still have issues in CI running headlessly. I've started #2609 to discuss a fix for this too.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Don't believe the last two points are necessary here but feel free to correct me.

@YousefBawaliz
Copy link

That seems to fix it. huge thanks man!
this issue has been bugging me for way too long now. Almost gave up FastAPI because of it.

let's hope it's merged as fast as possible.

@Kludex Kludex requested a review from Copilot April 13, 2025 10:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

uvicorn/supervisors/basereload.py:93

  • Relying on a print statement may lead to unexpected output buffering behavior in some environments. Consider using sys.stdout.write("") followed by sys.stdout.flush() to ensure immediate output and to avoid side effects in production.
print("")  # This helps trigger windows to respond to the Ctrl+C event

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

@Kludex Kludex changed the title Fix slow reload on Windows Flush the stdout buffer on Windows to trigger reload Apr 13, 2025
@Kludex Kludex enabled auto-merge (squash) April 14, 2025 08:27
@Kludex Kludex merged commit 86dc456 into encode:master Apr 14, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants