fix(daemon): probe a live daemon again before replacing it as unreachable

Merged
OK
2mo ago
+369-11

Summary

A client replaced a live daemon it could not reach within one 500 ms probe. That budget is wall-clock time on the client's own event loop: a client that stalls past it (a large synchronous parse, a GC pause on a loaded host) sees a listening daemon as unreachable. The takeover then kills it, and every session it held goes with it.

Downstream, the e2e mobile benchmark drives two iOS simulators through one daemon. In CI we saw Replacing daemon (pid N, v0.21.16) ...: unreachable mid-run. The other worker then failed its next command in one of three ways: snapshot failed: 2 devices match this request equally (its session was gone), Daemon request timed out, or Invalid daemon response.

readReusableLocalDaemon now probes up to three more times, 200 ms apart, before it decides a daemon is unreachable. Reachability on the client's transport is asked last, only when version and code identity leave the decision to it, and only while the recorded pid is alive (a signal-0 check, since the ps identity read misses its deadline under the same load). The takeover still proves identity before it signals. A dead daemon is still replaced at once. A recovered probe emits a daemon_probe_recovered diagnostic.

Touched: 2 source files (daemon-client-lifecycle.ts, daemon-launch-spec.ts), 1 new test file, 2 updated test files.

Validation

Tested commit 1aeb7b91d; on its merge with main, pnpm test:coverage:ci passed and the changed-line gate passed (92.3%).

  • A Node reproduction outside the repo: a synchronous stall of 600 ms or more right after the probe arms reports a listening loopback server as unreachable.
  • daemon-client-stalled-probe.test.ts: a first probe forced to miss keeps the live stand-in daemon, and daemon_probe_recovered names it. A daemon whose pid is gone gets exactly one probe before it is replaced. daemon-launch-spec.test.ts: a version mismatch never asks client-transport reachability. Each assertion fails when its line of the fix is removed.
  • pnpm check:affected --run: passed.