hapi icon indicating copy to clipboard operation
hapi copied to clipboard

docs(installation): add KillMode=process to runner systemd unit (closes #915)

Open heavygee opened this issue 5 months ago • 0 comments

Summary

Adds KillMode=process to the reference runner systemd unit in docs/guide/installation.md, plus a one-paragraph note explaining the contract. Closes #915 ("hub restart cascade-archives every idle agent session; sessions should be resilient to hub bouncing").

Root cause (full diagnosis on #915)

The runner code is already correct:

  • cli/src/runner/run.ts:454 spawns child agents with detached: true (comment: "Sessions stay alive when runner stops")
  • cli/src/runner/run.ts:1049-1075 (cleanupAndShutdown) does NOT iterate pidToTrackedSession or kill any tracked children
  • cli/src/api/apiMachine.ts:385-387 configures socket.io with reconnection: true and the disconnect handler does NOT exit

The runner is already designed as a long-lived process whose exit leaves agent sessions intact. But Node's detached: true calls setsid() (new process session), which does NOT escape the parent's systemd cgroup. Without an explicit KillMode, systemd defaults to control-group, which SIGTERMs every PID in the runner's cgroup whenever the unit stops - forcibly archiving every running session and contradicting the runner's detach contract.

Fix

 [Service]
 Type=simple
+KillMode=process
 ExecStart=/usr/local/bin/hapi runner start-sync
 Restart=always

With KillMode=process:

  1. systemctl restart hapi-runner.service SIGTERMs only the main runner PID
  2. The runner's cleanupAndShutdown runs (does not touch children)
  3. Children stay alive in their detached sessions
  4. New runner starts via Restart=always, reconnects to hub via socket.io, re-establishes control via the existing RPC layer

This is the minimum upstream change required for #915 acceptance criteria #1 (After systemctl restart hapi-hub.service, the number of sessions in lifecycleState='archived' is unchanged) when operators follow this reference unit.

What this still doesn't fix (separate issue forthcoming)

If the runner itself crashes/dies before its children, on next runner start the children are orphans (no parent control protocol). They'll continue running but the runner won't know about them. The complementary fix - runner scans for orphaned children OR queries sessions WHERE active=1 AND startedBy='runner' and re-establishes control / --resume - will be a separate issue and PR after this lands. Filing the issue now and holding the implementation PR until this one is merged.

Verification on a live system

Diagnosed and tested on a fork-side host running 93d00414 (upstream HEAD at PR-open time). systemd-cgls /system.slice/hapi-runner.service showed ~6 cursor-agent processes, 1 claude process, multiple bun runner workers, and assorted tool-call shells, all under the runner unit's cgroup despite each being spawned detached: true. Confirms cgroup membership is the killer; setsid doesn't escape it.

A 7-hour outage on this fork at 2026-06-16 03:05-09:58 BST was caused by exactly this mechanism (full timeline in #915 follow-up comment): a stack-switch tool's patient_drain timed out, ran systemctl stop hapi-hub.service, the local unit's Requires=hapi-hub.service (fork-local drift, not in upstream's reference) cascade-stopped the runner, KillMode=control-group killed the script that was about to run systemctl restart, hub stayed orphan-stopped. With this PR's fix in upstream's reference unit, operators following the install guide get the runner-survival contract by default; the fork-local Requires= drift would still cause cascade-stop but with KillMode=process the children would survive it.

AI-disclosure

Per CONTRIBUTING.md "AI-Generated Code Policy": drafted with claude-opus-4.7 as a peer agent during a fork-side post-mortem of the outage referenced above. Diagnosis cross-checked against the previous bot comment on #915 (which correctly identified detached: true and the absence of cascade-kill in upstream code, but couldn't pin the cgroup mechanism without runtime evidence from a live system).

Test plan

  • [ ] Operator-side: edit the unit, systemctl --user daemon-reload, then systemctl --user restart hapi-runner (or stop+start) and verify via systemctl-cgls that cursor-agent / claude PIDs persist across the bounce
  • [ ] No code/build changes; CI is docs-only

Made with Cursor

heavygee avatar Jun 16 '26 09:06 heavygee