Skip to content

fix(client,cli): keep the bundle task polled while records and fsyncs run - #2274

Merged
thiras merged 3 commits into
mainfrom
fix/2211-bundle-task-polled
Oct 2, 2026
Merged

thiras merged 3 commits into
mainfrom
fix/2211-bundle-task-polled

Conversation

@thiras

@thiras thiras commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What

This PR keeps the decdn bundle pull task polled while durable writes run, and adds diagnostics for the stalls in #2211 and #2230. It is PR 1 of 3.

A bundle pull runs every entry and lane as one future on the main thread (block_on, with no tokio::spawn by design). Synchronous fsyncs on that future, and one inline await in the scheduler, left every lane of the run unpolled at the same moment. The nodes then saw every stream go quiet together. That matches the 21 s client-side silence, followed by 12 lane faults on 3 providers, reported in #2211.

Summary

  • Ordered state writes off the runtime thread. A new command-wide OrderedWrites (crates/cli/src/commands/ordered_writes.rs) runs these writes on the blocking pool, one at a time, in queue order:

    • a lane's peer-store open records (record_open);
    • its fault records (record_failure);
    • its voucher-watermark redb commits.

    Ordering matters because an open that lands after a later fault clears that fault's stamp, and a watermark rebase replaces the lane row outright. How the queue behaves:

    • A worker runs only while writes are queued and exits when the queue is empty, so an idle command holds no thread.
    • A write that panics is logged, and the queue keeps draining.
    • If a shutting-down runtime drops a worker without running it, the queue drains on the thread that drops it.
    • decdn fetch and decdn bundle pull own the queue at command level. Before endpoint.close(), on every exit including a Ctrl-C, they wait for it, bounded by WRITE_DRAIN (30 s).
  • Watermark capture order. queue_face_watermarks reads the lane ledgers and queues the writes in one synchronous call. Commit order is therefore read order across entries, as long as every entry is polled on one task (buffer_unordered does this).

    • The awaited settle waits on a completion receiver. The drop guard drops it.
    • Lane handles that share one ledger produce one write. That write uses the lowest prior_amount across the handles, so an advance past a sibling's rebased baseline still persists.
  • Registry bootstrap off the runtime thread. bootstrap_nodes runs resolve_bootstrap on spawn_blocking. On each discovery, that function writes and fsyncs one peer record per registry node, then runs prune_and_cap.

  • Scheduler record flush beside the workers.

    • The 5 s .ranges write is now an in-flight future with its own select! arm, following the pattern of drive_with_interval_flush.
    • A tick that finds a write still in flight skips, and logs a debug line.
    • A write still in flight lands before the final flush, on both the success exit and the error exit. If a superseded write failed, that failure is only logged, and the final write's result decides.
  • Diagnostics.

    • A fresh leg logs its wait from open to first frame as phase="first_frame".
    • bundle pull runs a 1 s poll watch. It logs a WARN, "the bundle task was not polled for at least …", that points at a blocking call (or a system suspend) and suggests -vv. The lateness it reports is a lower bound.
  • Comments. The comment on CLIENT_RECEIVE_WINDOW now says that lanes to one node share that node's connection.

Root cause (file refs)

  • crates/client/src/discovery.rs, resolve_bootstrap: N peer-record fsyncs and a prune on each discovery, reached through CliSources::discover → resolve_target_node → bootstrap_nodes.
  • crates/cli/src/commands/cli_sources.rs, on_source_fault / record_open: one fsync per fault or open, made from a sync hook on the pull future.
  • crates/cli/src/commands/fetch.rs, watermark persistence: one redb fsync per lane handle at each entry end. The handles were not de-duplicated.
  • crates/client/src/scheduler.rs, the record tick arm: store.flush_present_record() was awaited inline, so no worker was polled while the write ran.

Refs #2211
Refs #2230

Reviewer notes

  • Read ordered_writes.rs first, then scheduler.rs (the record slot), then queue_face_watermarks in fetch.rs.
  • No pub item of decdn-client or decdn-protocol changed.
  • FlushCountingStore moved into the shared #[cfg(test)] doubles in source.rs. It now numbers each flush, and it can land its writes out of order, so a test can check which snapshot landed last.
  • Deviations from the plan:
    • There is no upsert_identity no-op skip. identity_seen_at_secs means "last confirmed against the registry" and drives identity_fresh / identity_prunable, so skipping the upsert would make it lie.
    • Writes are de-duplicated by ledger identity, not by lane key.
  • Follow-up, not in this PR: route spawn_harvest's record_sample writes through OrderedWrites as well. That code predates this PR, and it still runs on a tokio::spawn task.
  • Out of scope: the node's proof clock is PR 2, and one-shot lane growth is PR 3.

Verification

  • cargo fmt -- --check
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo nextest run -p decdn-client -p decdn-cli: 1372 passed
  • RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items
  • pre-commit run --files <changed files>
  • New tests. Each one was confirmed to fail against the bug or mutation it targets, unless noted otherwise:
    • scheduler:
      • a_slow_record_write_does_not_pause_the_lanes: bounded to 2 min; fails fast with the inline flush.
      • the_final_record_lands_last_when_the_fetch_completes
      • the_final_record_lands_last_when_the_fetch_gives_up
    • ordered_writes:
      • writes_run_in_queue_order_and_the_worker_exits_when_idle
      • a_panicking_write_does_not_strand_the_writes_behind_it
      • a_worker_the_runtime_drops_still_drains_the_queue
      • racing_submitters_never_strand_a_write
      • a_write_after_an_idle_spell_still_runs
    • fetch:
      • duplicate_lane_handles_commit_once_from_the_lowest_baseline
      • a_rebase_read_first_never_lands_over_a_newer_advance and a_stale_advance_read_first_never_lands_over_a_rebase: these pin the end state; they cannot deterministically catch a per-call spawn.
      • a_detached_settle_lands_by_runtime_shutdown: no mutation run for this one.
    • cli_sources: an_open_and_a_later_fault_from_two_entries_land_in_order
  • Live decdn bundle pull (Mistral or SDXL) with RUST_LOG=info,decdn_cli=debug,decdn_client=debug. Look for "the bundle task was not polled" and phase="first_frame". This runs after PRs 1–3 merge, before the issues close.
  • contracts/ changed: n/a
  • User-visible change: CHANGELOG.md entry added. n/a: this PR adds diagnostics and an internal fix only.
  • Design change: n/a
  • PR title uses an allowed type with a lowercase subject

🤖 Generated with Claude Code

https://claude.ai/code/session_01NHKbG6Jm75vFMLTNqK8ppz

Copilot AI balanced review requested due to automatic review settings October 1, 2026 21:06
@thiras thiras self-assigned this Oct 1, 2026

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.

Copilot review overview

🟡 Changes recommended

Write reordering, dropped watermark advances, and blocking-pool exhaustion remain unresolved.

Review effort: Balanced
Findings: 2 High severity · 2 Medium severity

Open (4)
What changed in this PR

Reduces bundle-pull stalls by moving durable writes off the shared pull task and polling record flushes alongside active lanes.

Changes:

  • Offloads peer records and buyer watermarks to blocking workers.
  • Deduplicates lane settlements and keeps scheduler flushes in flight.
  • Adds stall diagnostics and regression tests.
File Description
crates/​client/​src/​source.rs Shares the record-flush test double.
crates/​client/​src/​scheduler.rs Polls record writes beside lane workers.
crates/​client/​src/​lib.rs Logs first-frame waits.
crates/​client/​src/​endpoint.rs Corrects connection-sharing documentation only.
crates/​client/​src/​driver.rs Reuses the shared test double.
crates/​client/​src/​discovery.rs Offloads bootstrap peer-store writes.
crates/​cli/​src/​commands/​fetch.rs Offloads and deduplicates watermark persistence.
crates/​cli/​src/​commands/​cli_sources.rs Queues peer events and supports asynchronous settlement.
crates/​cli/​src/​commands/​bundle_pull.rs Adds a poll watchdog and asynchronous settlement.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/cli/src/commands/cli_sources.rs Resolved
Comment thread crates/cli/src/commands/fetch.rs Resolved
Comment thread crates/cli/src/commands/cli_sources.rs Resolved
Comment thread crates/cli/src/commands/fetch.rs Resolved
@github-code-quality

github-code-quality Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit a825a73 in the fix/2211-bundle-task... branch remains at 87%, unchanged from commit 3a565c1 in the main branch.

Show a line coverage summary of the most impacted files.
File main 3a565c1 fix/2211-bundle-task... a825a73 +/-
crates/client/src/source.rs 94% 93% -1%
crates/client/src/lib.rs 92% 92% 0%
crates/cli/src/.../bundle_pull.rs 81% 81% 0%
crates/client/src/discovery.rs 98% 98% 0%
crates/client/src/scheduler.rs 95% 95% 0%
crates/cache/src/engine.rs 94% 95% +1%
crates/cli/src/...mmands/fetch.rs 71% 73% +2%
crates/client/src/driver.rs 96% 98% +2%
crates/cli/src/.../cli_sources.rs 51% 53% +2%
crates/cli/src/...dered_writes.rs 0% 98% +98%

Updated October 02, 2026 02:57 UTC

thiras and others added 3 commits October 2, 2026 05:25
… run

A bundle pull runs every entry and lane as one future on the main
thread, so any synchronous fsync on that future stops every lane of
the run at once. The nodes then see all streams go quiet together and
time the legs out (#2211).

- discovery: resolve_bootstrap (one peer-record fsync per registry
  node, then prune_and_cap) runs on spawn_blocking.
- cli_sources: record_open / record_failure go to one ordered
  blocking-pool writer per fetch, so an open's cleared failure never
  lands after a later fault's stamp.
- fetch/bundle_pull: the buyer-progress redb commit runs on
  spawn_blocking (awaited on the normal path, detached from the drop
  guard); the store is shared through an Arc. Lane handles that share
  one ledger now produce one write per entry.
- scheduler: the 5 s .ranges record write runs beside the workers
  instead of being awaited inside the select arm; a write in flight
  lands before the final flush.
- diagnostics: a fresh leg logs its open-to-first-frame wait as
  phase="first_frame", and bundle pull warns "the bundle task was not
  polled for ..." when a 1 s tick fires more than 2 s late.
- endpoint: the connection receive-window comment reflects that lanes
  to one node share its connection.

Refs #2211
Refs #2230

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NHKbG6Jm75vFMLTNqK8ppz
…t baseline

Review follow-up on the off-runtime writes:

- One command-wide OrderedWrites queue (fetch: per command, bundle
  pull: on PullCtx, both through DriveFetchDeps) runs every peer record
  and voucher-watermark commit on the blocking pool, one at a time, in
  queue order. A worker runs only while writes are queued and exits when
  the queue empties, so an idle fetch parks no blocking-pool thread.
- Watermark writes are queued in the order the ledgers are read, for
  both the awaited settle and the drop guard's. Separate spawn_blocking
  jobs could commit a rebase over a newer advance, or a stale advance
  over a rebase, across concurrent entries.
- Peer records from different bundle entries share that order, so one
  entry's first open never clears another entry's later fault stamp.
- A ledger's one watermark takes the lowest prior_amount across its
  handles, so an advance past a sibling's rebased baseline persists.

Refs #2211
Refs #2230

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NHKbG6Jm75vFMLTNqK8ppz
…re exit

Review fixes for the off-runtime writes:

- OrderedWrites runs each write under catch_unwind, logs the panic and
  keeps draining, so a panicking write strands nothing behind it and an
  awaited settle cannot hang. A worker the runtime drops without running
  (shutdown) drains the queue on the dropping thread.
- fetch and bundle pull own the queue at command level and wait for it,
  bounded by WRITE_DRAIN, before endpoint.close() on every exit, a
  Ctrl-C's included, so a drop guard's watermark write is durable before
  the command returns.
- The ledger read and the watermark queueing are one pub(crate) call
  (queue_face_watermarks); the read-only and Vec-taking halves are
  private.
- The watermark-write warning names the lanes, pools and providers and
  the likely cause.
- scheduler: a superseded periodic record write's failure is logged and
  the final write decides; distinct post-loop warn; debug line on a
  skipped tick.
- discovery: the bootstrap JoinError keeps its source with context.
- The poll-watch WARN says the lateness is a lower bound and what to do.
- Docs and comments match the code (ordering precondition on one task,
  Ctrl-C durability, scope of the ordered writes, first_frame timing).

Tests: panic does not strand a later awaited write; a dropped worker
drains; racing submitters lose nothing; a detached settle lands by
runtime shutdown; the final record snapshot lands last on both the
success and give-up exits; the slow-record test is bounded to 2 min;
every new await is bounded.

Refs #2211
Refs #2230

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NHKbG6Jm75vFMLTNqK8ppz
@thiras
thiras force-pushed the fix/2211-bundle-task-polled branch from f3b34d3 to a825a73 Compare October 2, 2026 02:46
@thiras
thiras merged commit e64368f into main Oct 2, 2026
30 checks passed
@thiras
thiras deleted the fix/2211-bundle-task-polled branch October 2, 2026 03:05
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.

2 participants