Skip to content

fix(menubar): no-output watchdog, lock signal cleanup, dead-pid takeover, serve orphan reaping - #1120

Merged
iamtoruk merged 1 commit into
mainfrom
fix/1117-menubar-livelock
Aug 24, 2026
Merged

iamtoruk merged 1 commit into
mainfrom
fix/1117-menubar-livelock

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

Fixes #1117 — the 0.9.20 menubar refresh livelock (fixed 45s kill × leaked refresh lock × waiter-gives-up-before-stale-gate), diagnosed live on a 21k-file corpus where the menubar had not completed a single fetch since upgrade.

A — Swift watchdog (port of #1096's electron semantics, same constants)

CLIWatchdog.swift: pure verdict type — 45s silence window (warm), 10min cold floor (raises the silence window, still finite: a child that never emits a byte dies at 10min), 15min hard ceiling, SIGTERM→5s→SIGKILL. Spawns set CODEBURN_PROGRESS=1 and any stdout/stderr activity re-arms the window; progress frames filtered out of error text. ServeConnection warm-up loses its fixed 60s cap (keepalive-driven + ceiling), and ensureStarted() gets a re-entry path: the death budget resets after a 300s cooldown, so a transient triple-death no longer kills the resident serve for the app's lifetime.

B — Lock signal cleanup

cache-refresh-lock.ts arms SIGINT/SIGTERM cleanup exactly as session-cache does for hydrating.lock (unlink-if-ours, re-raise, armed once, cleared on release).

C — Waiter can outlive an abandoned lock

Abandoned = heartbeat mtime older than staleMs OR recorded pid dead (kill(pid,0)/EPERM probe, win32-safe). Applied to the wait loop, takeover, and the takeover-guard file. DEFAULT_WAIT_MS deleted — the default wait is now staleMs + 30s so the two can never drift out of order again. Live holders are safe by construction: heartbeat inside staleMs AND live pid; takeover still requires an unchanged observation under the exclusive guard. The lock wait itself now emits keepalives (it was the one legitimately-silent stretch that would have tripped the new window).

D — Serve orphan prevention (root causes found in code)

  1. Retiring a generation nil'd the handle but the pipe's write end stayed open — the child never saw EOF. Now closed explicitly on all three retirement paths, all escalating SIGTERM→SIGKILL.
  2. applicationWillTerminate's Task { await shutdown() } almost certainly never ran before exit — quit-time cleanup was effectively absent. Now a synchronous lock-guarded ServeChildRegistry reap.
  3. Crash path: pidfile reaper ported from electron's reapOrphanServe (suffix argv match — the shebang rewrite makes exact match impossible; documented).
  4. CLI: real process.exit bound before runStdioServe (an in-flight request past the drain bound left a monkeypatched exit that threw), post-drain watcher await bounded.

Verification (re-run after rebasing onto clean main)

swift build clean, 193 Swift tests / 28 suites (+10 new incl. cold-floor-still-kills and stderr-keepalive cases) · tsc clean · test:locks 31/31 (agent ran it 3× during development, no flakes; 3 of 4 new lock tests fail with the fix reverted — proven non-vacuous) · full CLI suite 3200 green · app suite untouched and green.

Ships in 0.9.21: this is the mac-app half; the CLI half (#1097 partial saves + the perf stack) is already on main. All three surfaces (CLI, desktop, menubar) release together.

…ver, serve orphan reaping

The 0.9.20 menubar never completed a fetch on a large corpus: the cache
version bump forced a full rehydration, DataClient's fixed 45s kill ended
it mid-transaction, cache-refresh-lock had no signal cleanup so the dead
holder's lock survived, and DEFAULT_WAIT_MS (30s) < DEFAULT_STALE_MS (90s)
meant no waiter could ever recover it.

A. Swift port of #1096's watchdog. CLIWatchdog holds the constants and the
   pure verdict; spawns and the resident serve child set CODEBURN_PROGRESS=1;
   the window restarts on any stdout/stderr byte; 45s silence, 10min cold
   floor until the first payload, 15min ceiling, SIGTERM then SIGKILL after
   5s. ServeConnection's fixed 60s warm cap becomes the same silence window,
   re-armed by each progress frame, and a spent death budget is now a
   5-minute cooldown instead of disabling the resident for the app run.

B. cache-refresh-lock arms SIGINT/SIGTERM cleanup the way session-cache does
   for hydrating.lock, so a SIGTERMed holder unlinks its own lock.

C. Staleness also opens on a dead holder pid, and the waiter budget derives
   from staleMs so it can never expire before the gate it waits for. A live
   holder - fresh heartbeat, pid answers signal 0 - is never taken from.
   parser.ts heartbeats through the lock wait, the one silent stretch left.

D. The app closes its end of a retired child's stdin (dropping the handle
   left the pipe alive inside the Process), reaps every serve child
   synchronously at quit, and records pid+argv so a crash-orphaned child is
   reaped next launch. serve's final exit no longer runs through a
   monkeypatched process.exit, and its post-drain cleanup is bounded.

Closes #1117
@iamtoruk
iamtoruk merged commit 84c4a97 into main Aug 24, 2026
16 checks passed
@iamtoruk
iamtoruk deleted the fix/1117-menubar-livelock branch August 24, 2026 08:35
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.

bug: menubar 0.9.20 refresh livelock — fixed 45s kill + leaked refresh lock + waiter gives up before stale gate

1 participant