mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-20 05:05:14 +02:00
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.
89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
#
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
API routes for compute notifications.
|
|
"""
|
|
|
|
import logging
|
|
from typing import Union
|
|
|
|
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
|
|
from websockets.exceptions import ConnectionClosed, WebSocketException
|
|
|
|
from gns3server.compute.notification_manager import NotificationManager
|
|
|
|
from .dependencies.authentication import ws_compute_authentication
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.websocket("/notifications/ws")
|
|
async def project_ws_notifications(websocket: Union[None, WebSocket] = Depends(ws_compute_authentication)) -> None:
|
|
"""
|
|
Receive project notifications about the project from WebSocket.
|
|
"""
|
|
|
|
if websocket:
|
|
client = f"{websocket.client.host}:{websocket.client.port}" if websocket.client else "unknown"
|
|
log.info(f"New client {client} has connected to compute WebSocket")
|
|
try:
|
|
with NotificationManager.instance().queue() as queue:
|
|
while not websocket.app.state.exiting:
|
|
notification = await queue.get_json(5)
|
|
await websocket.send_text(notification)
|
|
except (ConnectionClosed, WebSocketDisconnect):
|
|
log.info(f"Client {client} has disconnected from compute WebSocket")
|
|
except WebSocketException as e:
|
|
log.warning(f"Error while sending to controller event to WebSocket client: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from starlette.responses import HTMLResponse
|
|
|
|
app = FastAPI()
|
|
app.include_router(router)
|
|
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<body>
|
|
<ul id='messages'>
|
|
</ul>
|
|
<script>
|
|
var ws = new WebSocket("ws://localhost:8000/notifications/ws");
|
|
ws.onmessage = function(event) {
|
|
var messages = document.getElementById('messages')
|
|
var message = document.createElement('li')
|
|
var content = document.createTextNode(event.data)
|
|
message.appendChild(content)
|
|
messages.appendChild(message)
|
|
};
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
@app.get("/")
|
|
async def get() -> HTMLResponse:
|
|
return HTMLResponse(html)
|
|
|
|
uvicorn.run(app, host="localhost", port=8000)
|