mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-20 13:15: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
+25
-9
@@ -42,6 +42,12 @@ from gns3server.version import __version__
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Uvicorn waits for open connections forever by default. A long-lived
|
||||
# streaming response (for example the MCP GET/SSE stream) would then block
|
||||
# Ctrl+C indefinitely. Bound the wait so remaining tasks are cancelled and
|
||||
# the lifespan shutdown (controller and project cleanup) always runs.
|
||||
GRACEFUL_SHUTDOWN_TIMEOUT = 10
|
||||
|
||||
|
||||
class Server:
|
||||
_stream_handler = None
|
||||
@@ -222,6 +228,24 @@ class Server:
|
||||
log.critical("Can't write pid file %s: %s", path, str(e))
|
||||
sys.exit(1)
|
||||
|
||||
@staticmethod
|
||||
def _create_uvicorn_config(app, host: str, port: int, access_log: bool) -> uvicorn.Config:
|
||||
"""
|
||||
Build the uvicorn configuration.
|
||||
"""
|
||||
|
||||
server_config = Config.instance().settings.Server
|
||||
return uvicorn.Config(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
access_log=access_log,
|
||||
ssl_certfile=server_config.certfile,
|
||||
ssl_keyfile=server_config.certkey,
|
||||
lifespan="on",
|
||||
timeout_graceful_shutdown=GRACEFUL_SHUTDOWN_TIMEOUT,
|
||||
)
|
||||
|
||||
async def run(self, parser, args):
|
||||
|
||||
self._setup_logging(args)
|
||||
@@ -301,15 +325,7 @@ class Server:
|
||||
if config.Server.enable_ssl:
|
||||
log.info("SSL is enabled")
|
||||
|
||||
config = uvicorn.Config(
|
||||
app,
|
||||
host=host,
|
||||
port=port,
|
||||
access_log=access_log,
|
||||
ssl_certfile=config.Server.certfile,
|
||||
ssl_keyfile=config.Server.certkey,
|
||||
lifespan="on",
|
||||
)
|
||||
config = self._create_uvicorn_config(app, host, port, access_log)
|
||||
|
||||
# overwrite uvicorn loggers with our own logger
|
||||
for uvicorn_logger_name in ("uvicorn", "uvicorn.error"):
|
||||
|
||||
Reference in new issue
Block a user