Repository navigation
fix(daemon): pre-bind probe — short-circuit second-wave spawn herd (#640 partial) - #641
Merged
Merged
Conversation
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]>
This was referenced Jun 4, 2026
This was referenced Jul 9, 2026
2 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()returnsNoneand 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 (anArcSwap<DepGraph>instate.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)returnstrueiff: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).endpointcompletes withintimeout(500 ms inrun_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_serverspins 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_fileprobe_returns_false_when_lock_file_records_self_pidprobe_returns_false_when_lock_file_pid_is_not_a_daemonEnd-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
soldr cargo check -p zccache --testscleansoldr cargo clippy -p zccache --tests -- -D warningscleansoldr cargo fmt --all -- --checkcleanwrapper-e2e (windows-latest)(the lane that caught v1 of fix(daemon): pipe-in-use bind error → INFO + exit 0 (partial #637) #639). This PR'srun_serverrestructure must not introduce the same "pipe bound but no accept" problem v1 of fix(daemon): pipe-in-use bind error → INFO + exit 0 (partial #637) #639 did. It shouldn't, because the probe runs BEFORE the bind and exits if the bind would lose — the winning daemon's bind+accept happens on the same timeline as before.Refs: #640 (partial — covers second wave), follows-up #639
🤖 Generated with Claude Code