Skip to content

perf(daemon): defer depgraph load to spawn_blocking — closes #640 herd - #648

Merged
zackees merged 1 commit into
mainfrom
perf/defer-depgraph-load-640
Jun 4, 2026
Merged

zackees merged 1 commit into
mainfrom
perf/defer-depgraph-load-640

Conversation

@zackees

@zackees zackees commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

The first-wave daemon herd from #637 / #640 finally collapses. With #642's ArcSwap<DepGraph> foundation in place, set_dep_graph is &self + atomic-swap-safe, so the on-disk depgraph load can fire from tokio::task::spawn_blocking AFTER server.run() has started the accept loop.

Order of operations on the winning daemon

Step Before After this PR
1 crash guard, "starting" log crash guard, "starting" log
2 Pre-bind probe (#641) Pre-bind probe (#641)
3 defender banner defender banner
4 load depgraph (3+ s) tokio runtime built
5a tokio runtime built DaemonServer::bind(endpoint) — pipe claimed at t ≈ 0
5b (still in rt.block_on) bind On bind error: #639 path
5c inject graph, run accept write spawn event + lock file (winner-only)
5d spawn_blocking(load_depgraph_and_install) — parallel with accept
5e server.run() — accept loop runs immediately
5f (background) load completes, DepGraphSetter::install swaps in

Compile requests arriving during step (5e) before (5f) see the empty default DepGraph and take the cold-path miss. That is correct — the cache_system's stat-verify safety net catches any stale-hit risk; we lose ~3 s of cache hits per cold start, which is exactly what the user was paying anyway under the old broken-herd flow.

Order of operations on redundant daemons

The 277-daemon herd shape collapses to ~2 across both waves:

Why this didn't ship in v1 of #639

The first attempt at bind-before-load broke wrapper-e2e (windows-latest): the pipe was bound but the accept loop hadn't started, so clients connected fast, sent a message, then timed out after 10 s waiting for a response that no one was reading.

This PR keeps the bind + accept on the same timeline. Both happen inside rt.block_on; only the depgraph load moves to a background task. Accept starts at the same instant it always did relative to bind. The v1 #639 regression shape doesn't recur.

The reason this couldn't be done before #642: set_dep_graph used Arc::get_mut, which only works pre-run(). ArcSwap removed that constraint. This PR is exactly the follow-up #642's commit message promised.

New types / API

  • daemon::DepGraphSetter — owned handle that survives spawn_blocking boundaries. Holds a clone of the daemon's Arc<SharedState>; calls state.dep_graph.store(...) + state.depgraph_load_warning.blocking_lock() on install().
  • DaemonServer::dep_graph_setter() -> DepGraphSetter — public method to hand out the setter before calling run().
  • DaemonServer::set_depgraph_load_warning is now &self + uses tokio::sync::Mutex::blocking_lock (matching the field's existing type). Same change shape as set_dep_graph in refactor(state): wrap state.dep_graph in ArcSwap — foundation for #640 #642.

Test plan

  • soldr cargo check -p zccache --tests clean
  • soldr cargo clippy -p zccache --tests -- -D warnings clean
  • soldr cargo fmt --all -- --check clean
  • soldr cargo test -p zccache --lib — 1693 / 1693 pass
  • PR CI green — particularly wrapper-e2e (windows-latest). The v1 fix(daemon): pipe-in-use bind error → INFO + exit 0 (partial #637) #639 regression was "bound pipe, no accept" for 3+ s. This PR keeps bind and accept on the same timeline (both inside rt.block_on), so the regression shape should not recur.

A live-spawn-race repro is intrinsically flaky as a unit test (depends on real Windows pipe scheduling), so coverage is the structural one: "is there any blocking I/O between rt.block_on(async) entry and DaemonServer::bind?" — answer after this PR: no.

Closes #640

🤖 Generated with Claude Code

The first-wave daemon herd #637 / #640 finally collapses. With #642's
ArcSwap<DepGraph> foundation in place, `set_dep_graph` is `&self` +
atomic, so the on-disk depgraph load can fire from
`tokio::task::spawn_blocking` AFTER `server.run()` has started the
accept loop.

## Order of operations on a winning daemon (after this PR)

Cold start, daemon binary started by a CLI spawn:

1. crash guard, "starting" log
2. **Pre-bind probe (#641)** — quick check for an existing serving daemon
3. defender banner
4. tokio runtime built
5. `rt.block_on(async { ... })`:
   a. **`DaemonServer::bind(endpoint)` — pipe claimed at t ≈ 0**
   b. On bind error: #639's PermissionDenied/AlreadyExists/AddrInUse
      → INFO + exit 0; other → ERROR + exit 1
   c. **Winner-only**: write spawn event + lock file
   d. **`tokio::task::spawn_blocking(load_depgraph_and_install)`**
      — fires in parallel with accept loop
   e. Ctrl+C handler spawned
   f. **`server.run(idle_timeout)` — accept loop runs immediately**
   g. When the spawn_blocking task finishes (~3 s on a populated
      cache), it calls `DepGraphSetter::install(graph, warning)`
      which `ArcSwap::store`s the new graph

Compile requests arriving during step (f) but before (g) see the
empty default `DepGraph` and take the cold-path miss. That is
correct — the `cache_system`'s stat-verify safety net catches any
stale-hit risk; we lose ~3 s of cache hits per cold start, which is
exactly what the user was paying anyway under the old broken-herd
flow.

## Order of operations on a redundant daemon

The 277-daemon herd shape from #637 collapses to ~2 across both
waves of the user's repro:

- **First wave** (initial cohort, no lock file yet): N daemons all
  race at step 5a. One wins; the other N-1 hit #639's
  PermissionDenied path at step 5b in microseconds and exit 0.
  Critically, none of them paid the depgraph load cost — the bind
  is the first I/O after process start.
- **Second wave** (spawned by clients whose connect failed mid-load
  on the winner): caught at step 2 by #641's pre-bind probe.

## Why this didn't ship in v1 of #639

The first attempt at bind-before-load broke
`wrapper-e2e (windows-latest)`: the pipe was bound but accept hadn't
started, so clients connected fast, sent a message, then timed out
after 10 s waiting for a response that no one was reading. This PR
keeps the bind + accept on the same timeline (both inside
`rt.block_on`); only the depgraph load moves to a background task.
Accept starts at the same instant it always did relative to bind.

The reason this couldn't be done before #642: `set_dep_graph` used
`Arc::get_mut`, which only works pre-`run()`. ArcSwap removed that
constraint. This PR is exactly the follow-up #642's commit message
promised.

## New types / API

- `daemon::DepGraphSetter` — owned handle that survives
  `spawn_blocking` boundaries. Holds a clone of the daemon's
  `Arc<SharedState>`; calls `state.dep_graph.store(...)` +
  `state.depgraph_load_warning.blocking_lock()` on `install()`.
- `DaemonServer::dep_graph_setter() -> DepGraphSetter` — public
  method to hand out the setter before calling `run()`.
- `DaemonServer::set_depgraph_load_warning` is now `&self` + uses
  `tokio::sync::Mutex::blocking_lock` (matching the field's existing
  type). Same change shape as `set_dep_graph` in #642.

## Tests

- All 1693 pre-existing lib tests pass.
- Live-spawn-race repro is intrinsically flaky as a unit test (depends
  on real Windows pipe scheduling), so coverage is the structural
  one: "is there any blocking I/O between `rt.block_on(async)` entry
  and `DaemonServer::bind`?" — answer after this PR: no.

## 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] `soldr cargo test -p zccache --lib` — 1693 / 1693 pass
- [ ] PR CI green — particularly `wrapper-e2e (windows-latest)`. The
  v1 #639 regression was "bound pipe, no accept" for 3+ s. This PR
  keeps bind and accept on the same timeline (both inside
  `rt.block_on`), so the regression shape should not recur.

Closes #640

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit 01a2947 into main Jun 4, 2026
31 checks passed
@zackees
zackees deleted the perf/defer-depgraph-load-640 branch June 4, 2026 05:10
zackees added a commit that referenced this pull request Jun 4, 2026
Ships #649 (zccache m e s o n configure wrapper, closes #627), #650 (perf: single-pass output-meta build), #651 (test: cross-key contention), #648 (closes #640). The headline payload is #649 — the wrapper was merged 2026-06-04 but the 1.11.11 release was tagged 2026-06-03, so downstream pyproject pins on >=1.11.11 do not currently see it. This release closes that gap. Validated end-to-end against FastLED on Windows.
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.

perf(daemon): defer depgraph load to spawn_blocking so bind can run first — full #637 herd collapse

1 participant