fix(dashboard): suppress Ctrl+C shutdown traceback

This commit is contained in:
Emiya·Leon 2026-06-26 16:12:18 +08:00 committed by Austin Pickett
parent 423f92e607
commit 931e1f7b15
2 changed files with 30 additions and 6 deletions

View File

@ -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

View File

@ -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)