mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-19 20:55:14 +02:00
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:
9 files changed
+110
-31
No files matched your search
@@ -67,6 +67,7 @@ compute_api = FastAPI(
|
||||
)
|
||||
|
||||
compute_api.state.controller_host = None
|
||||
compute_api.state.exiting = False
|
||||
|
||||
|
||||
@compute_api.exception_handler(ComputeError)
|
||||
|
||||
@@ -44,7 +44,7 @@ async def project_ws_notifications(websocket: Union[None, WebSocket] = Depends(w
|
||||
log.info(f"New client {client} has connected to compute WebSocket")
|
||||
try:
|
||||
with NotificationManager.instance().queue() as queue:
|
||||
while True:
|
||||
while not websocket.app.state.exiting:
|
||||
notification = await queue.get_json(5)
|
||||
await websocket.send_text(notification)
|
||||
except (ConnectionClosed, WebSocketDisconnect):
|
||||
|
||||
@@ -308,7 +308,7 @@ async def controller_ws_notifications(
|
||||
log.info(f"New client {client} has connected to controller WebSocket")
|
||||
try:
|
||||
with Controller.instance().notification.controller_queue() as queue:
|
||||
while True:
|
||||
while not websocket.app.state.exiting:
|
||||
notification = await queue.get_json(5)
|
||||
await websocket.send_text(notification)
|
||||
except (ConnectionClosed, WebSocketDisconnect):
|
||||
|
||||
@@ -659,7 +659,7 @@ async def project_ws_notifications(
|
||||
log.info(f"New client has connected to the notification stream for project ID '{project.id}' (WebSocket method)")
|
||||
try:
|
||||
with controller.notification.project_queue(project.id) as queue:
|
||||
while True:
|
||||
while not websocket.app.state.exiting:
|
||||
notification = await queue.get_json(5)
|
||||
await websocket.send_text(notification)
|
||||
except (ConnectionClosed, WebSocketDisconnect):
|
||||
@@ -701,7 +701,7 @@ async def project_marker_ws_notifications(
|
||||
)
|
||||
try:
|
||||
with controller.notification.project_marker_queue(project.id) as queue:
|
||||
while True:
|
||||
while not websocket.app.state.exiting:
|
||||
notification = await queue.get_json(5)
|
||||
await websocket.send_text(notification)
|
||||
except (ConnectionClosed, WebSocketDisconnect):
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user