fix(cli): kill uvicorn worker tree on Ctrl+C

With --workers > 1, SIGINT from the terminal raced uvicorn's supervisor:
some workers got signaled directly, the supervisor respawned them, and
the result behaved like a forkbomb. Start uvicorn in its own session and
signal the whole process group (SIGTERM → 10s grace → SIGKILL) when we
catch KeyboardInterrupt.
This commit is contained in:
2026-04-17 15:32:08 -04:00
parent 342916ca63
commit bb8d782e42

View File

@@ -90,6 +90,7 @@ def api(
import subprocess # nosec B404 import subprocess # nosec B404
import sys import sys
import os import os
import signal
if daemon: if daemon:
log.info("API daemonizing host=%s port=%d workers=%d", host, port, workers) log.info("API daemonizing host=%s port=%d workers=%d", host, port, workers)
@@ -101,9 +102,22 @@ def api(
_env["DECNET_INGEST_LOG_FILE"] = str(log_file) _env["DECNET_INGEST_LOG_FILE"] = str(log_file)
_cmd = [sys.executable, "-m", "uvicorn", "decnet.web.api:app", _cmd = [sys.executable, "-m", "uvicorn", "decnet.web.api:app",
"--host", host, "--port", str(port), "--workers", str(workers)] "--host", host, "--port", str(port), "--workers", str(workers)]
# Put uvicorn (and its worker children) in their own process group so we
# can signal the whole tree on Ctrl+C. Without this, only the supervisor
# receives SIGINT from the terminal and worker children may survive and
# be respawned — the "forkbomb" ANTI hit during testing.
proc = subprocess.Popen(_cmd, env=_env, start_new_session=True) # nosec B603 B404
try: try:
subprocess.run(_cmd, env=_env) # nosec B603 B404 proc.wait()
except KeyboardInterrupt: except KeyboardInterrupt:
try:
os.killpg(proc.pid, signal.SIGTERM)
try:
proc.wait(timeout=10)
except subprocess.TimeoutExpired:
os.killpg(proc.pid, signal.SIGKILL)
proc.wait()
except ProcessLookupError:
pass pass
except (FileNotFoundError, subprocess.SubprocessError): except (FileNotFoundError, subprocess.SubprocessError):
console.print("[red]Failed to start API. Ensure 'uvicorn' is installed in the current environment.[/]") console.print("[red]Failed to start API. Ensure 'uvicorn' is installed in the current environment.[/]")