From 931e1f7b153e166ac0e8d213937d753fb891b62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emiya=C2=B7Leon?= Date: Fri, 26 Jun 2026 16:12:18 +0800 Subject: [PATCH] fix(dashboard): suppress Ctrl+C shutdown traceback --- hermes_cli/web_server.py | 18 ++++++++++++------ tests/test_web_server.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py index a533fbc2a5964..7ef912b2c3ce5 100644 --- a/hermes_cli/web_server.py +++ b/hermes_cli/web_server.py @@ -18226,11 +18226,14 @@ def start_server( if server.started: await server.shutdown() - # On POSIX, keep the long-standing ``asyncio.run(_serve())`` behavior - # unchanged — Python's default loop there is already a SelectorEventLoop - # (or uvloop when uvicorn[standard] installs it), which is exactly what - # uvicorn serves on. Touching that path would only widen the blast radius - # for no benefit. + # On POSIX, keep the long-standing ``asyncio.run(_serve())`` runner — + # Python's default loop there is already a SelectorEventLoop (or uvloop when + # uvicorn[standard] installs it), which is exactly what uvicorn serves on. + # Uvicorn's ``capture_signals()`` restores the original SIGINT handler and + # re-raises the captured signal after a graceful shutdown, which otherwise + # leaks a noisy KeyboardInterrupt traceback for the normal foreground + # dashboard Ctrl+C path. Treat that one signal as a clean user-requested + # shutdown; other serve-time errors still propagate. # # On Windows it is broken: ``asyncio.run`` defaults to a ProactorEventLoop, # but uvicorn's socket-serving stack assumes a SelectorEventLoop on win32 @@ -18242,7 +18245,10 @@ def start_server( # no TCP handshake completing (#50641). So *only on Windows* we mirror # uvicorn's own machinery and run on the loop factory it picks. if sys.platform != "win32": - asyncio.run(_serve()) + try: + asyncio.run(_serve()) + except KeyboardInterrupt: + return return # Windows-only path. Resolve the runner + loop factory FIRST (and fall back diff --git a/tests/test_web_server.py b/tests/test_web_server.py index 55534e8b84e9a..84a1022d024e1 100644 --- a/tests/test_web_server.py +++ b/tests/test_web_server.py @@ -243,3 +243,21 @@ def test_start_server_keeps_bare_asyncio_run_on_posix(monkeypatch): assert runner_called["hit"] is False, ( "POSIX must not take the Windows loop-factory branch" ) + + +def test_start_server_treats_posix_keyboardinterrupt_as_clean_shutdown(monkeypatch): + """Ctrl+C is the normal foreground-dashboard shutdown path. + + Uvicorn re-raises captured SIGINT as ``KeyboardInterrupt`` after it has + restored the original signal handlers. The dashboard should treat that as a + clean user-requested shutdown instead of leaking a traceback to the terminal. + """ + _stub_uvicorn(monkeypatch) + + def _raise_keyboard_interrupt(coro): + coro.close() + raise KeyboardInterrupt + + monkeypatch.setattr(asyncio, "run", _raise_keyboard_interrupt) + + web_server.start_server(host="127.0.0.1", port=0, open_browser=False)