Daemon-owned last-exit retirement for OMP hosts

Closed
QI
2mo ago
+636-10

Problem

A supervised daemon started by the OMP lifecycle extension was retired by the extension itself on the last host's session_shutdown. When that hook never fires — taskkill, a crash, a terminal close — the daemon, its supervised modules, and their grandchildren all leaked, surviving every host.

Change

The daemon now owns its own retirement.

  • holder_monitor.rs (new) — ticks every 2s, gated on OMP_SUBC_OWNED=1 and Windows, so standalone, launchd, and test daemons are untouched. When every lease holder is gone it retires the supervised trees, removes the connection file, and exits the process.
  • supervise.rs — retire_tree(): captures the child pid before retire() destroys the handle, then taskkill /T /F so grandchildren die with the module instead of surviving it.
  • bootstrap.rs — the monitor task joins the serve select, so retiring the daemon tears the listener and watchdog down cleanly.

Lock ordering

Lease fingerprints are read and probed unlocked. The registrar lock is taken only once every holder is gone, and it reaps a stranded lock itself (read → probe owner liveness → re-read to confirm no change → remove → create_new). Two leases_unchanged re-verifies then gate retirement — one under the boundary lock, one under the operation lock — so a host that registers mid-retirement aborts it.

This matters because every host must win that same lock (wx, 250ms retry, hard 20s throw) to register or release a lease. Probing under the lock would block host registration for up to one 5s CIM probe per lease per tick on slow WMI.

Fail-closed throughout: an unparseable lease, a failed probe, or a null identity reads as a live holder and blocks retirement.

Verification

  • Forced close (two hosts): first-host kill preserves the remaining holder and the service tree; last-host force-kill retires daemon + module + grandchild; an abandoned registrar lock is recovered. 3/3.
  • Graceful: cold start → shared daemon → first-exit preserve → last-exit retire (daemon exits 0, ~2s) → restart → retire. Full sequence passes.
  • 276/276 lib tests, tests/watchdog.rs integration green, clippy --all-targets -D warnings clean.

Regressions run against a freshly built ck-subc.exe in isolated run directories; the production daemons were not restarted for this PR.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes leaked OMP daemons by moving last-host retirement from the lifecycle extension's session_shutdown hook into the daemon itself. Previously a supervised daemon survived forever when that hook never fired (taskkill, crash, terminal close); now the daemon drains and retires its supervised trees, removes its connection file, and exits once every lease holder is gone.

Bug Fixes

  • New holder_monitor.rs ticks every 2 s, gated on Windows and OMP_SUBC_OWNED=1; OMP_SUBC_STOP_ON_LAST_EXIT=0 opts out, so standalone, launchd, and test daemons are untouched.
  • retire_tree() drains before taskkill /T /F, so graceful-stop work reaches a live process and grandchildren die with the module.
  • Lease probes avoid the registrar lock to keep registration responsive; leases are re-verified under it so a holder registering mid-retirement aborts.
  • Fail-closed: a failed probe, unparseable lease, or null process identity counts as a live holder and blocks retirement.
  • Adds docs/specs/subc-lease-contract.md documenting the lease, lock, and retirement contract.