fix: bound graceful shutdown and stop notification WebSockets on exit

By default uvicorn waits for open connections forever. A long-lived
streaming response keeps the server waiting after Ctrl+C. One common
case is the MCP Streamable HTTP transport: the client opens a
standalone GET request and the server answers with an SSE stream
(not the old SSE transport). A second Ctrl+C sets force_exit. Uvicorn
then skips the lifespan shutdown, so project and controller cleanup
does not run.

Add timeout_graceful_shutdown. After the timeout uvicorn cancels the
remaining tasks and runs the lifespan shutdown. Also make the four
notification WebSocket handlers stop when app.state.exiting is set.
The HTTP stream handlers already do this.
This commit is contained in:
YueGuobin committed 2026-11-04 00:32:34 +08:00
1 parent bfe25641fc
commit 52c19002f2
9 files changed
+110 -31

No files matched your search

+2
View File
@@ -134,6 +134,8 @@ unicorn_exit_handler = UvicornServer.handle_exit
def handle_exit(*args, **kwargs):
app.state.exiting = True
# The compute API is a mounted sub-app with its own State object.
compute_api.state.exiting = True
unicorn_exit_handler(*args, **kwargs)