Skip to content

fix(daemon): pre-bind probe — short-circuit second-wave spawn herd (#640 partial) - #641

Merged
zackees merged 1 commit into
mainfrom
fix/daemon-prebind-probe-640
Jun 4, 2026
Merged

zackees merged 1 commit into
mainfrom
fix/daemon-prebind-probe-640

Conversation

@zackees

@zackees zackees commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Catches the redundant-daemon herd cause from #637 / #640 without requiring the full ArcSwap refactor sketched in #640.

Before paying the 3+ s depgraph-load cost, the daemon checks whether another daemon is already serving at the endpoint. If yes, exit 0 cleanly without writing a spawn event, lock file, or doing any heavy init.

What this catches

#637 measured 277 redundant daemon spawns over a 3-hour parallel FastLED build. Spawn intervals ranged from 11 ms to ~10 s — most of those are second-wave spawns, fired by clients that connected to a busy/loading daemon, gave up, and spawned their own. With this PR each second-wave daemon detects the existing daemon in ~50-500 ms (timeout-bounded probe) and exits cleanly.

What this does NOT catch

The first-wave cohort racing for the bind from a cold start. No lock file exists yet, so read_lock_file_pid() returns None and the probe short-circuits as "no existing daemon". Those first-wave losers are still caught by the post-bind PermissionDenied → INFO + exit 0 path landed in #639. The first-wave collapse needs the deferred-depgraph-load refactor tracked in #640 (an ArcSwap<DepGraph> in state.dep_graph + every reader migrating to .load() is ~200-400 LOC; this PR is the safe wedge that doesn't require server-side changes).

How it works

ipc::probe_existing_daemon(endpoint, timeout) returns true iff:

  1. The lock file records a PID.
  2. The PID is alive AND its executable is zccache-daemon (verify_daemon_pid, which already defended against recycled PIDs per zccache 1.3.10 daemon reported unresponsive at startup in fbuild CI (Linux) #132).
  3. An IPC connect to endpoint completes within timeout (500 ms in run_server).

The connection from a successful probe is dropped immediately — we only verify that the other end is accepting, not exchange any application-level message.

run_server spins up a short-lived single-thread tokio runtime just for the probe (~ms), then drops it. The full multi-thread runtime is still built later, only for the daemon that won the race.

pid == std::process::id() short-circuits early so a sibling racing-init thread that wrote our own PID into the lock file under pathological conditions cannot deadlock us into waiting for ourselves to accept.

Tests

Three unit tests in crates/zccache/src/ipc/mod.rs::tests:

Test What it pins
probe_returns_false_when_no_lock_file empty state — fast no-op
probe_returns_false_when_lock_file_records_self_pid self short-circuit prevents self-deadlock
probe_returns_false_when_lock_file_pid_is_not_a_daemon PID 1 (init / System) — verify_daemon_pid short-circuits BEFORE connect timeout (asserted via elapsed < 250 ms vs 500 ms timeout)

End-to-end "live daemon + probe defers" needs a real serving daemon, same shape as the existing integration tests; the spawn-herd repro is intrinsically flaky as a unit test.

Test plan

Refs: #640 (partial — covers second wave), follows-up #639

🤖 Generated with Claude Code

 partial)

Catches the redundant-daemon herd cause from #637 / #640: before
paying the 3+ s depgraph-load cost, check whether another daemon is
already serving at the endpoint. If yes, exit 0 cleanly without
writing a spawn event, lock file, or doing any heavy init.

## What this catches

The user's #637 measurement: 277 redundant daemon spawns over 3 hours
during a parallel FastLED build. Spawn intervals ranged from 11 ms to
~10 s — most of those are **second wave** spawns, fired by clients
that connected to a busy/loading daemon, gave up, and spawned their
own. With this PR each second-wave daemon detects the existing daemon
in ~50–500 ms (timeout-bounded probe) and exits cleanly.

## What this does NOT catch

The **first-wave** cohort racing for the bind from a cold start — no
lock file exists yet, so `read_lock_file_pid()` returns `None` and
the probe short-circuits as "no existing daemon". Those first-wave
losers are still caught by the post-bind PermissionDenied → INFO +
exit 0 path landed in #639. The full first-wave collapse needs the
deferred-depgraph-load refactor tracked separately in #640 (an
ArcSwap<DepGraph> in `state.dep_graph` + every reader migrating to
`.load()` is ~200-400 LOC; this PR is the safe wedge that doesn't
require server-side changes).

## How it works

`ipc::probe_existing_daemon(endpoint, timeout)` returns `true` iff:

1. The lock file records a PID.
2. The PID is alive AND its executable is `zccache-daemon`
   (`verify_daemon_pid`, which already defended against recycled PIDs
   per #132).
3. An IPC connect to `endpoint` completes within `timeout`.

The connection from a successful probe is dropped immediately — we
only verify that the other end is accepting, not exchange any
application-level message.

`run_server` spins up a short-lived single-thread tokio runtime just
for the probe (~ms), then drops it. The full multi-thread runtime is
still built later, only for the daemon that won the race.

`pid == std::process::id()` short-circuits early so a sibling racing-
init thread that wrote our own PID into the lock file under
pathological conditions cannot deadlock us into waiting for ourselves
to accept.

## Tests

Three unit tests in `crates/zccache/src/ipc/mod.rs::tests`:

- `probe_returns_false_when_no_lock_file` — empty state.
- `probe_returns_false_when_lock_file_records_self_pid` — self
  short-circuit.
- `probe_returns_false_when_lock_file_pid_is_not_a_daemon` — PID-1
  case (init / System), verifies the verify_daemon_pid short-circuit
  fires BEFORE the connect timeout (asserted via elapsed < 250 ms vs
  500 ms timeout).

End-to-end "live daemon + probe defers" is harder to encode as a
unit test (needs a real serving daemon, which is what the
integration test harness exercises) and is the same shape as the
existing daemon spawn lifecycle tests — the spawn-herd repro is
intrinsically flaky as a unit test.

## Test plan

- [x] `soldr cargo check -p zccache --tests` clean
- [x] `soldr cargo clippy -p zccache --tests -- -D warnings` clean
- [x] `soldr cargo fmt --all -- --check` clean
- [x] 3 new probe tests pass
- [ ] PR CI green (including the `wrapper-e2e (windows-latest)` lane
  that caught v1 of #639 — this PR's restructure of `run_server`
  must not break the smoke test the same way)

Refs: #640 (partial — covers second wave), follows-up #639

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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