Skip to content

test(cold-path): crash-mid-flight recovery test for #610 - #618

Merged
zackees merged 1 commit into
mainfrom
test/deferred-cold-path-crash-recovery
Jun 2, 2026
Merged

zackees merged 1 commit into
mainfrom
test/deferred-cold-path-crash-recovery

Conversation

@zackees

@zackees zackees commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fourth installment of #610's adversarial test track.

crash_mid_flight_recovery_never_surfaces_wrong_content simulates a daemon crash mid-flight by dropping the DaemonServer (no graceful shutdown — no shutdown.notify(), no final WAL flush, no in-flight task drain) and binding a fresh server to the same cache root.

Invariant under test

The post-restart compile lookup either:

  • Hits with content matching the original cold-miss bytes (artifact + index were already durable before the crash), or
  • Misses and recompiles to the same deterministic bytes (artifact and/or index were lost).

It never surfaces content from a different cache key. That is the wrong-hit window DD-015 forbids and DD-025 condition 2 ("failure mode is a miss, never a wrong hit") explicitly preserves under deferred-write semantics.

Why this matters for #610

Under #610's deferred-write path, the response leaves the daemon before the in-memory cache becomes visible and before the WAL flush. A crash that lands inside that window must not leave any future lookup with corrupt or cross-key content. The test asserts that today (the sync-store path already meets the bar via DD-008/DD-010/DD-017 — atomic writes + ACID redb + persistent artifacts) and will continue to assert it once the defer lands.

Test mechanics

  • CacheDirEnvGuard pins ZCCACHE_CACHE_DIR to a tempfile::tempdir for both daemon instances → they share the same cache root.
  • First DaemonServer::bind does the cold-miss compile.
  • First server is dropped (no graceful path). Any uncommitted in-flight WAL entries are lost intentionally.
  • Second DaemonServer::bind loads from on-disk state and handles a fresh compile request for the same source.
  • Assertion: bytes match.

Refs

Test plan

  • CI green
  • No production code touched

🤖 Generated with Claude Code

Fourth installment of #610's adversarial test track.

`crash_mid_flight_recovery_never_surfaces_wrong_content` simulates a
daemon crash mid-flight by dropping the `DaemonServer` (no graceful
shutdown — no notify, no final WAL flush, no in-flight task drain) and
binding a fresh server to the same cache root.

The invariant: the post-restart compile lookup either hits with content
matching the original cold-miss bytes (artifact + index were durable
before the crash) or recompiles from scratch to the same deterministic
bytes (artifact and/or index were lost). It never surfaces content from
a different cache key.

This is the canonical recovery invariant DD-025 names: under the
deferred-write path, the response leaves the daemon BEFORE the in-
memory cache becomes visible and BEFORE the WAL flush. If a crash
lands inside that window, the next daemon's `load_all()` from the on-
disk index plus content-addressed artifact directory must recover any
committed entry, and reject any uncommitted one without surfacing
wrong content.

Both daemons share the cache root via `ZCCACHE_CACHE_DIR`; the
second daemon reads whatever the first managed to persist before the
abrupt drop.

Refs #605, #610, DD-025.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@zackees zackees mentioned this pull request Jun 2, 2026
@zackees
zackees merged commit d0dbdda into main Jun 2, 2026
26 checks passed
@zackees
zackees deleted the test/deferred-cold-path-crash-recovery branch June 2, 2026 18:32
zackees added a commit that referenced this pull request Jun 5, 2026
…refs #610) (#664)

Adds the `pending_cache_writes` registry on `SharedState` and the
`pending_writes` helper module (`register` / `complete` /
`await_pending`). This is the **named sync point** required by
DD-025 condition 1 — the bridge between the daemon's response-return
and the deferred publication of a cold-miss artifact into
`state.artifacts` that the follow-up PR will introduce in
`store_*` (see `daemon/server/handle_compile/miss_store.rs`).

Lands the **notify-timeout fall-through** adversarial test pair
(unit + `SharedState`-integration) — the remaining DD-025
condition 4 item identified in PR #618. The wiring follows the
existing #610 "tests first, wiring second" cadence (PRs #611,
#612, #613, #618, #651).

## What this PR does

- Adds `SharedState.pending_cache_writes: DashMap<String, Arc<Notify>>`
  (keyed by artifact_key_hex), initialized empty at bind time.
- Adds `daemon/server/pending_writes.rs` with three helpers:
  - `register(pending, key) -> Arc<Notify>` — idempotent: repeat
    calls for the same key return the same handle.
  - `complete(pending, key)` — notify waiters + remove entry.
  - `await_pending(pending, key, timeout) -> bool` — caps the
    caller-supplied timeout at `PENDING_WAIT_TIMEOUT` (5 ms) so a
    buggy caller cannot blow the DD-025 condition-3 blast radius.
    Returns `true` if a pending entry was observed (caller should
    re-attempt the lookup), `false` if not (caller should fall
    through to its normal miss path).
- Unit tests (5) in the module + integration tests (4) in
  `tests/pending_cache_writes.rs` covering:
  - `register` idempotency for the same key
  - `complete` wakes waiters and removes the entry
  - notify-timeout fall-through within blast-radius bound
  - warm lookup (no pending entry) pays zero wait
  - caller-supplied timeout is capped at the blast-radius bound
- `#[allow(dead_code)]` on the helpers + field with a comment
  pointing at the follow-up wiring PR. The lib compiles clean
  under `-D warnings`.

## What this PR explicitly does NOT do

- It does not yet defer `dep_graph.update` or
  `state.artifacts.insert`. The hot path is unchanged.
- It does not yet call `await_pending` from
  `lookup_artifact_with_disk_fallback` (the lookup function is
  sync; routing it through the registry requires the follow-up
  PR's async refactor or a thin async wrapper at the call site).

The follow-up PR will plug the registry into both sides and lift
the `#[allow(dead_code)]` annotations.

## Test plan

- [x] `soldr cargo test -p zccache --lib -- pending` — 9/9 new
  tests pass (+13 unrelated `pending` tests already green)
- [x] `soldr cargo check --workspace --all-targets`
- [x] `soldr cargo clippy --workspace --all-targets -- -D warnings`
- [x] `soldr cargo fmt --all --check`

Refs #610, #605. DD-025.
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