Skip to content

fix(node): derive pull-through outer deadline so multi-provider fallback survives timeout-shaped failures (#859) - #878

Merged
thiras merged 2 commits into
mainfrom
fix/859-pull-through-fallback-timeout
Jun 11, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/859-pull-through-fallback-timeout

Conversation

@thiras

@thiras thiras commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Problem

Closes #859. Node-to-node cache-miss pull-through's advertised N-provider fallback was dead under timeout-shaped failures. The outer delivery deadline (ClientHandler::try_pull_through, wrapping the whole discover → probe → rank → pull fetch in one tokio::time::timeout) and the per-candidate stream timeout (NodeOriginConfig.pull_timeout) were both sourced from the same cache.node_pull_timeout_sec value. So when candidate #1 stalled, both clocks expired together and the outer timeout cancelled the entire NodeOrigin::fetch at the instant candidate #1's own timeout fired — candidates #2..N (MAX_PROVIDER_ATTEMPTS = 3) were never tried. Fallback only worked for fast failures (connect refused, immediate error). Introduced with #831/#834.

Fix

Part 1 — derived outer deadline (Strategy A). Keep node_pull_timeout_sec as the documented per-upstream budget; derive the outer deadline as MAX_PROVIDER_ATTEMPTS × per + PULL_THROUGH_OUTER_SLACK via the new selection::outer_pull_deadline, wired at runtime/mod.rs attach_pull_through. The outer backstop can no longer preempt the fallback loop. Zero regression risk for a slow-but-healthy single upstream; no new config knob.

Part 2 — background cache-fill. When the foreground deadline fires, the handler spawns a detached, budget-bounded, CancellationToken-cancelled task to keep warming the cache from a slow-but-available upstream for future requests (leaning on CacheEngine::populate's existing single-flight). A handler-layer in-flight set deduplicates per hash; the claim and its release are fused into one Option<BgInflightGuard> so the dedup invariant is type-enforced. Cancelled at shutdown alongside gossip_shutdown. New metrics: node_pull_through_background_{spawned,succeeded,failed}.

Tests

  • pull_through_outer_deadline_accommodates_a_slow_pull (handler-level regression) — drives a real authorized request against a SlowOrigin (1.5s); asserts the store stays empty under the pre-node: pull-through outer deadline equals the per-candidate stream timeout — multi-provider fallback is dead under timeout-shaped failures #859 outer == per wiring but is filled under the derived deadline. The one test that fails if attach_pull_through regresses to the per-candidate value.
  • node_origin_pull_falls_through_a_stalled_candidate — a staller that ranks first, answers the probe, then stalls the stream; asserts the loop abandons it (scored Unreachable) and delivers from the honest fallback (scored clean).
  • outer_pull_deadline_exceeds_all_per_candidate_budgets + edges — the arithmetic root-cause guard (outer > N × per_candidate, saturation).
  • background_fill_dedup_and_rearm — claim → dedup → re-arm-after-release contract.

Review

A multi-agent review (code/tests/comments/silent-failures/type-design) ran on the branch; all Critical/Important findings were addressed — including the originally-too-weak regression test, a swallowed cache.has store fault (now metered+logged), and the arm/guard fusion. Deferred (advisory, noted): full background-fill behavioral suite and a dedicated node_pull_background_timeout_sec knob.

Verification

  • cargo clippy -p decdn-node -p decdn-common -p decdn-cli --all-targets — clean
  • cargo nextest run -p decdn-node — 573 passed; -p decdn-cli -p decdn-common — 539 passed
  • cargo fmt --check — clean; pre-commit (fmt/clippy/doc/deny) — passed

🤖 Generated with Claude Code

…ack survives timeout-shaped failures (#859)

The cache-miss pull-through outer deadline (`try_pull_through`) and the
per-candidate stream timeout (`NodeOriginConfig.pull_timeout`) were both
sourced from `cache.node_pull_timeout_sec`, so they expired together. When
candidate #1 stalled, the outer `tokio::time::timeout` cancelled the whole
`NodeOrigin::fetch` before the `MAX_PROVIDER_ATTEMPTS` loop reached candidates
#2..N — the advertised N-provider fallback only worked for fast failures.

Fix (Strategy A): keep `node_pull_timeout_sec` as the per-upstream budget and
derive the outer deadline as `MAX_PROVIDER_ATTEMPTS × per + slack` via
`selection::outer_pull_deadline`, so the outer backstop can never preempt the
fallback loop. The config field keeps its documented per-upstream meaning;
no new knob.

Also adds background cache-fill: when the foreground deadline fires, the
handler spawns a detached, budget-bounded, cancellation-token'd task to keep
warming the cache from a slow-but-available upstream (deduped per hash by a
guard-released in-flight set; cancelled at shutdown alongside gossip). Three
new metrics: node_pull_through_background_{spawned,succeeded,failed}.

Tests: a handler-level regression test proving the slow pull is abandoned
under the old equal-deadline wiring but completes under the derived deadline;
a NodeOrigin stall-fallthrough test; the derivation guard
(outer > N × per_candidate); and the background-fill dedup contract.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 11, 2026 00:25
@thiras thiras self-assigned this Jun 11, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a detached background cache-fill mechanism to warm the cache from slow upstreams after a foreground delivery deadline expires, and updates the outer pull-through deadline derivation to prevent premature preemption of the provider fallback loop. It also adds corresponding metrics, configuration updates, and comprehensive tests. The review feedback suggests enhancing the resilience of the background fill lock management by properly handling poisoned mutexes in both the BgInflightGuard drop implementation and the arm_background_fill function.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread crates/node/src/handlers/client.rs Resolved
Comment thread crates/node/src/handlers/client.rs Resolved
@github-actions

github-actions Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 85.87% (was 85.93%, ▼ 0.06%) — 34407/40069 lines

File Coverage Lines Δ
crates/cli/src/commands/config.rs 67.26% 152/226 ● 0.00%
crates/common/src/config/mod.rs 96.83% 6009/6206 ● 0.00%
crates/node/src/handlers/client.rs 75.07% 545/726 ▼ 1.25%
crates/node/src/metrics.rs 83.65% 711/850 ▼ 0.54%
crates/node/src/runtime/mod.rs 49.66% 878/1768 ▼ 0.23%
crates/node/src/selection.rs 97.85% 546/558 ▲ +0.08%
History
Commit Total Δ vs base
b80c755 85.87% ▼ 0.06%
192aabb (HEAD) 85.87% ▼ 0.06%

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes node-to-node cache-miss pull-through fallback being prematurely cancelled under timeout-shaped failures by deriving a larger outer pull-through deadline from the per-candidate budget, and adds an optional detached background cache-warm path to continue populating the cache after the foreground deadline fires.

Changes:

  • Derive handler-level outer pull-through deadline as MAX_PROVIDER_ATTEMPTS × per_candidate + slack via selection::outer_pull_deadline, and wire it in runtime.
  • Add handler-driven detached background cache-fill on foreground timeout, with per-hash dedup and new metrics.
  • Add regression tests covering stalled-first-candidate fallthrough, derived outer deadline behavior, and background-fill dedup.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/node/src/selection.rs Adds outer_pull_deadline + slack constant and arithmetic regression tests.
crates/node/src/runtime/mod.rs Wires derived outer deadline into attach_pull_through and attaches background-fill with shutdown cancellation.
crates/node/src/handlers/client.rs Implements background-fill plumbing + timeout-path race handling and metrics/logging updates.
crates/node/src/metrics.rs Adds counters for background fill spawned/succeeded/failed and helper increment methods.
crates/node/tests/node_origin_pull.rs Adds stalled-provider integration test and a configurable per-candidate timeout helper.
crates/node/tests/client_loopback.rs Adds handler-level regression test ensuring derived outer deadline allows slow pull to complete.
crates/common/src/config/types.rs Clarifies node_pull_timeout_sec as a per-upstream budget (outer is derived).
crates/common/src/config/mod.rs Updates default timeout docs to reflect derived outer deadline semantics.
crates/cli/src/commands/config.rs Updates example config comment to describe derived overall deadline behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/node/src/handlers/client.rs Resolved
Comment thread crates/node/src/handlers/client.rs Resolved
…t over-count & log wording (#859)

Code-review follow-ups on the background cache-fill path:

- BgInflightGuard::drop and arm_background_fill now recover a poisoned
  dedup lock via PoisonError::into_inner instead of silently skipping
  cleanup / declining to spawn — the claim is always released and the
  feature stays functional after an unrelated panic. (Gemini)
- node_pull_through_timeout is now metered AFTER the race `has` check, so
  a blob that lands in the store right as the deadline fires is served
  without over-counting an abandoned-pull timeout. (Copilot)
- The background-fill failure log no longer says "found no source" for a
  store/I/O fault; the message is neutral and the cause rides in `error`.
  (Copilot)

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@thiras
thiras merged commit 14c7c7d into main Jun 11, 2026
20 checks passed
@thiras
thiras deleted the fix/859-pull-through-fallback-timeout branch June 11, 2026 00:44
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.

node: pull-through outer deadline equals the per-candidate stream timeout — multi-provider fallback is dead under timeout-shaped failures

2 participants