fix(host-service): dispose standalone hosts on shutdown signals

VI
8h ago
+381-123

What & why

Addresses the standalone shutdown defect identified in #7426. The health-check starvation reported there remains open; this change does not attribute it to WAL growth.

serve.ts takes no disposer from createApp() and installs signal handlers only in development. A production SIGTERM/SIGINT therefore exits without closing the app's SQLite connection, workers, and watchers.

Handle both signals in production and development. Stop the listener and relay using the existing stop-serving path, stop the terminal daemon only in development, then await the app disposer before exiting. An 8-second deadline and a repeated signal force an exit if asynchronous cleanup gets stuck, within the CLI's 10-second escalation window. Cleanup failures are logged and do not skip later steps.

The production path keeps the existing terminal-daemon lifecycle. No API or database schema changes are needed.

How I tested it

  • Seven focused tests cover cleanup order, development-only daemon cleanup, errors in each cleanup step, deadline escalation, and repeated signals. They use an injected deadline and do not wait for real timeouts.
  • Process probe bundles the actual serve.ts in production mode with isolated boot dependencies, then runs it under Node 22.23.1 with a real HTTP server and SQLite database. The fixture substitutes external integrations and supplies a disposer that closes SQLite; it does not start an installed host or daemon.
SignalOriginal entry pointFixed entry point
SIGTERMdisposer not called; WAL 12,392 bytesdisposer called; WAL removed; exit 0
SIGINTdisposer not called; WAL 12,392 bytesdisposer called; WAL removed; exit 0

The inserted row remains readable in every run. The daemon-stop callback is never called in production. Probe source and captured results. This is process-level verification with controlled dependencies, not a reproduction of the reporter's loaded Linux host.

  • bun run lint:fix, then bun run lint: pass, 13,608 files.
  • bun run typecheck: 42/42 tasks pass.
  • TERM=xterm-256color bun run test --continue=always: 27/30 tasks pass. Desktop: 9,651 pass, 0 fail. Host-service: 2,919 pass, 1 existing history-ordering failure tracked by #9713; the shutdown tests pass. Shared has the known macOS date-format failure (#9658), and agent-setup has unchanged shell-wrapper/OpenCode test failures. The earlier run before rebasing passed all 2,920 host-service tests. No CI or unrelated test changes are included.

A completely blocked main event loop still needs the CLI's external SIGKILL escalation. The shutdown deadline handles asynchronous cleanup that does not settle, matching the existing desktop shutdown strategy from #7145.

Implementation, diff inspection, tests, and process probes were performed with Codex. No human review or manual testing is claimed.

Checklist

  • PR title follows conventional commits (type(scope): subject)
  • bun run lint and bun run typecheck pass (CI fails on lint warnings too)
  • "Allow edits from maintainers" is checked on fork PRs
  • I have signed the CLA (confirmed on #9744 and #9771)

Summary by cubic

Fixes standalone host-service shutdown so SIGTERM/SIGINT close the app's SQLite connection, workers, and watchers before exiting, and a failed startup runs the same cleanup before exiting non-zero.

  • Runs the shutdown sequence in production and development: stop serving, stop the dev daemon (development only), then dispose the app.
  • Forces exit after an 8-second deadline or a repeated signal, within the CLI's 10-second escalation window.
  • Logs cleanup failures without skipping remaining steps, reports startup errors during cleanup, and cancels cleanup if startup is aborted mid-way.
  • Switches to the Hono adaptor server to keep the configured hostname on the listener.
  • Adds tests covering cleanup order, startup failures, failing steps, deadline escalation, and repeated signals.

Summary by CodeRabbit

  • Bug Fixes
    • Improved shutdown handling so serving activity and connections are cleaned up in order before the process exits.
    • Cleanup continues if an individual shutdown step fails, helping prevent resources from being left open.
    • Shutdown now has a time limit and responds to a repeated stop signal with a hard exit, preventing the process from hanging indefinitely.
    • Startup failures are reported during shutdown, and stop signals can interrupt startup safely.
    • Development shutdown waits for daemon startup to finish before stopping the supervisor.

Latest rebase validation (7ad33aeb206c, base d31b92a172d4): 7 shutdown tests pass; repository lint and host-service typecheck pass. The real Node/SQLite signal probe again closes the database and removes the WAL for SIGTERM and SIGINT. The full-suite results above are from the previous tested base.