-
-
Notifications
You must be signed in to change notification settings - Fork 786
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
Flush the stdout buffer on Windows to trigger reload #2604
Conversation
That seems to fix it. huge thanks man! let's hope it's merged as fast as possible. |
There was a problem hiding this 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
uvicorn/supervisors/basereload.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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("")
thensys.stdout.flush()
- No success here, it still doesn't trigger a reload.sys.stdout.write("\n")
thensys.stdout.flush()
- This is closer toprint("")
and does work. But at that point, I'm not sure it's any better than just usingprint("")
There was a problem hiding this comment.
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)
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:
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
Don't believe the last two points are necessary here but feel free to correct me.