Skip to content

Exit host daemon promptly after clean shutdown - #2965

Merged
ymichael merged 4 commits into
bb/ship-host-only-machine-artifact-thr_9dvdk3y5xcfrom
bb/investigate-modal-sandboxes-for-bb-thr_q76bgsd4em
Sep 2, 2026
Merged

ymichael merged 4 commits into
bb/ship-host-only-machine-artifact-thr_9dvdk3y5xcfrom
bb/investigate-modal-sandboxes-for-bb-thr_q76bgsd4em

Conversation

@ymichael

@ymichael ymichael commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The production host daemon had no deterministic process exit after its semantic shutdown completed. It relied on every Node resource allowing the event loop to drain or on a 15-second watchdog, while the machine installer allows the temporary daemon only five seconds before sending SIGKILL. Lingering resources could therefore turn an otherwise clean shutdown into a forced kill and delay the service-manager handoff.

What changed

After event flushing, runtime shutdown, and lock release all succeed, the production daemon now clears its watchdog and exits immediately with the caller-selected status. Normal shutdowns exit with status 0, while daemon-lock loss exits with status 1 through both clean and watchdog paths.

Startup failure is tracked separately from a shutdown requested while startup is pending. A real startup failure still reaches the entrypoint's diagnostic and failure path, while SIGINT or SIGTERM during pending startup exits promptly and cannot subsequently report the daemon as started. A cleanup step that genuinely hangs retains the existing 15-second watchdog.

This does not change the host-daemon wire contract, so HOST_DAEMON_PROTOCOL_VERSION is unchanged. There are no CLI or documentation changes.

How you verified

  • pnpm exec turbo run test --filter=@bb/host-daemon --force: 46 files and 568 tests passed.
  • pnpm exec turbo run test --filter=@bb/host-daemon --force -- --run src/daemon.test.ts: 11 tests passed, including clean exit, caller-selected lock-loss status, pending-startup signal handling, hung-cleanup watchdog, and startup-failure behavior.
  • pnpm exec turbo run typecheck --filter=@bb/host-daemon --force: passed.
  • pnpm exec turbo run build --filter=bb-app --force: 12 tasks passed.
  • Started the rebuilt bb-app host-daemon join against a fresh local server, waited until connected, sent SIGTERM, and measured 274 ms to exit with status 0 and no forced-kill warning.
  • Direct child-process checks confirmed daemon-lock loss exits with status 1 and SIGTERM during deliberately blocked startup exits with status 0 in 140 ms despite a 60-second watchdog and an active interval.

AGENT GENERATED

@ymichael

ymichael commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop

@bb-slop-cop

bb-slop-cop Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I am the Slop Cop. I am reviewing this pull request for security, code quality, performance, tests, and duplicate architecture.

Comment thread apps/host-daemon/src/daemon.ts Resolved
Comment thread apps/host-daemon/src/daemon.ts Resolved

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 SLOP COP 🚨 · review

Plain-English summary: This change makes the host daemon end immediately after successful cleanup. It no longer waits for active Node resources or the 15-second watchdog.

I found two shutdown problems.

  1. A daemon-lock loss must exit with status 1. The new status-0 exit occurs before the existing .finally() callback can run. A child-process test confirmed status 0.
  2. A signal during onStart skips the immediate exit because started remains false. With an active resource, the process waits for the watchdog. A process test took 207 ms with a 200 ms grace value.

The security review found no new trust boundary, input path, privilege, credential, network, or data-access risk.

I found no duplicate daemon shutdown code. The server and desktop have different process owners and exit rules. The daemon should make one exit decision with a caller-selected status. That design also gives startup failure and startup interruption separate meanings.

The focused daemon lifecycle test passed all nine tests. The host-daemon type check passed. All current GitHub checks completed successfully or had a configured skip.

This change has no browser route, so a Doobie test cannot observe it. The child-process tests directly covered the affected process behavior.

I would fix both shutdown cases before merge.

@ymichael

ymichael commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop

@bb-slop-cop

bb-slop-cop Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I am the Slop Cop. I am reviewing this pull request now.

I will check security, code quality, performance, architecture, and duplicate code. I will also test the changed path when possible.

Comment thread apps/host-daemon/src/daemon.ts Resolved
Comment thread apps/host-daemon/src/daemon.ts Resolved

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 SLOP COP 🚨 · review

Plain-English summary: This change makes the host daemon exit immediately after successful cleanup. It keeps status 1 for a daemon-lock failure.

I found two merge blockers.

  1. The required exitCode argument was not added to two integration-harness calls. The full integration type check and GitHub build fail.
  2. The first shutdown request owns the final status. A later daemon-lock loss cannot change an active status-0 shutdown to status 1.

My child-process check reproduced the second problem. A status-0 shutdown began, then a lock loss occurred before cleanup ended. The process exited with status 0.

The security review found no new privilege, input, credential, network, or data-access risk. Cleanup still occurs before a normal process exit.

I found no useful shared refactor. The server and desktop shutdown paths have different owners and exit rules. I found no stale forceExit name.

The host-daemon test suite passed all 568 tests. The host-daemon type check passed. The integration type check failed at helpers/harness.ts lines 455 and 479.

This change has no browser route, so Doobie cannot test it. The lifecycle tests and child-process check directly cover the changed behavior.

I posted two inline comments with the required fixes. I used a comment review and did not approve or request changes.

@ymichael

ymichael commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator Author

Ubuntu 24.04 systemd canary passed on the PR head (0e771c45f2).

  • Created a fresh arm64 Ubuntu 24.04 VM and installed Node 22.19.0.
  • Enrolled it with the unmodified /install.sh served by a fresh server built from this PR.
  • Full enrollment, package install, temporary-daemon handoff, systemd installation, and readiness completed in 15.815 s.
  • Verified the installed daemon bundle SHA-256 exactly matched the checkout build: 8de74126f4a5f4c8e738922f7706f9ba53bf6a60ab9876733653daea38b33571.
  • Sent SIGTERM to the systemd-managed daemon while connected:
    • clean process exit: 643 ms
    • replacement PID: 2.657 s
    • connected status restored: 2.938 s
    • systemd restart delta: exactly 1
  • The service ended in active/running with Result=success, and both its local status endpoint and the server reported the host connected.
  • Targeted installer, daemon, and journal scans found no watchdog force-exit, SIGKILL, or forceKilled: true event.

The disposable VM and fresh test server were removed after the run.

@ymichael
ymichael force-pushed the bb/investigate-modal-sandboxes-for-bb-thr_q76bgsd4em branch from 0e771c4 to 7685dd0 Compare September 2, 2026 23:16
@ymichael
ymichael changed the base branch from main to bb/ship-host-only-machine-artifact-thr_9dvdk3y5xc September 2, 2026 23:16
@ymichael
ymichael merged commit b333f3d into main Sep 2, 2026
15 checks passed
@ymichael
ymichael deleted the bb/investigate-modal-sandboxes-for-bb-thr_q76bgsd4em branch September 2, 2026 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant