Skip to content

fix(node): keep a partly paid chunk owed so the serve loop cannot spin (#2132) - #2133

Merged
thiras merged 1 commit into
mainfrom
fix/2132-partial-credit-spin
Sep 23, 2026
Merged

thiras merged 1 commit into
mainfrom
fix/2132-partial-credit-spin

Conversation

@thiras

@thiras thiras commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #2132. Two testnet nodes (fra1, lon1) wedged at 100% CPU. A proof that paid only part of an outstanding chunk made the cache-hit serve loop spin without awaiting, one tokio worker per stream.

Root cause. credit_advance caps a credit at lane headroom, so a sealed voucher that moves the watermark by a sliver credits a sliver. Both recoup loops added the sliver to paid but popped the whole chunk from pending. That broke Σ pending + unvouchered == delivered − paid. Once delivered − paid filled the window with pending empty, no branch of deliver awaited. Any paying client could trigger it on purpose.

Changes

  • OwedChunk { len, remaining } (voucher.rs) replaces the bare delta in both queues (delivery.rs, serve_leg.rs).
    • A chunk leaves the queue only when proofs have paid all of it.
    • MAX_PROOFS_PER_CHUNK counts every proof that leaves the chunk unsettled, so sliver after sliver hits the bound.
  • Whole-chunk rule kept. voucher_credit_delta classifies by len, not remaining, so a metering voucher still pays no part of a whole chunk (the fix: unstall bundle pull (runtime I/O, lane credit, ingest fsync, probe limits, boundary legs) #2124 anti-starvation rule).
  • Accounting checked in release builds. Both loops check after each recoup that delivered − paid equals the bytes not yet cut into a chunk (ensure_unpaid_bytes_tracked), and settle refuses an over-credit. Either break fails the stream as a node fault.
  • No-progress guard in both loops is now a node fault. It is logged at error! and counted in decdn_serve_stream_node_fault_total. The miss path used to file it as a client abandon, so ServeStop::ClientAbandoned is removed.
  • ADR 003 / 005:
    • A partly paid chunk stays owed.
    • An honest payer never gets a partial credit.
    • Rule 1 said the payer anchors a stream "at stream open", but the client does it lazily, immediately before the stream's first reveal. The ADRs now match the code, and the fix depends on that order.

Why no "wake on headroom" mechanism

An honest payer never meets a partial credit. Every voucher on a lane is issued from one shared PoolLedger as the lane frontier plus the stream's own delta, and the node's byte claim is exact. So each proof finds headroom for its own chunk. After a stream's last reveal, the payer sends only that stream's closing voucher.

Only a payer that claims fewer bytes than it received can get a partial credit: a sliver attacker, or a lane seeded from a stale base. Such a payer now stalls and then faults, instead of taking bytes it did not pay for. a_sub_chunk_stream_on_a_live_chain_sends_only_its_closing_voucher pins the client ordering this depends on. A mutation that sends an eager anchor fails it.

Tests

  • Cache hit:
    • a_sliver_voucher_leaves_the_rest_of_the_chunk_owed
    • sliver_vouchers_exhaust_the_per_chunk_proof_budget, which also asserts no node fault
    • a_chunk_settled_by_the_last_proof_of_its_budget_is_paid
  • Miss path:
    • window_pull_through_sliver_voucher_leaves_the_rest_owed, which runs to a fully paid StreamEnd
    • window_pull_through_sliver_vouchers_exhaust_the_proof_budget
  • Unit: OwedChunk settle and over-credit, the accounting check, and the whole-chunk rule on a partly paid chunk.
  • Red before the fix: the cache-hit sliver tests hang (the spinning worker wedges the test runtime, as in production). The miss-path one receives an unpaid 16 KiB runt frame.
  • Adjusted existing test: two_hashes_reuse_one_warm_connection reused a zero-based PoolContext for a second fetch on one lane, so it underpaid by about 400 KB and relied on the leak. It now seeds the second fetch from the lane's totals. The production client already does this through its shared ledgers.
  • Results: cargo nextest run -p decdn-node passes all 1258 tests. Workspace clippy, cargo doc -D warnings and fmt are clean.

Operational note

A wedged node still looks active to systemd. fra1 and lon1 need a manual restart until this ships.

🤖 Generated with Claude Code

#2132)

A proof credits at most the lane headroom it finds, so a sealed voucher
that moves the watermark by a sliver pays a sliver of the chunk. Both
recoup loops added that sliver to `paid` but popped the whole chunk, so
the unpaid rest was tracked nowhere. On a cache hit, once it filled the
credit window, `deliver` looped without awaiting and held one tokio
worker per stream; two such streams wedged a 2-vCPU testnet node.

- `OwedChunk { len, remaining }` replaces the bare delta in both queues.
  A chunk leaves the queue only when proofs have paid all of it, and
  `MAX_PROOFS_PER_CHUNK` counts every proof that leaves it unsettled.
- `voucher_credit_delta` classifies by the chunk's delivered length, so
  a metering voucher still pays no part of a whole chunk (#2124).
- Both loops check after each recoup that `delivered - paid` equals the
  bytes not yet cut into a chunk, and `settle` refuses an over-credit.
  Either break fails the stream as a node fault.
- The no-progress guard in both loops is a node fault. The miss path
  filed it as a client abandon; `ServeStop::ClientAbandoned` is gone.
- ADR 003/005: the payer anchors a stream immediately before its first
  reveal, not at stream open, and an honest payer never gets a partial
  credit. Both statements match the client and are now load-bearing.
- `two_hashes_reuse_one_warm_connection` reused a zero-based context for
  a second fetch on one lane and relied on the leak; it now seeds the
  second fetch from the lane's totals.

Tests: sliver credit, proof budget and last-proof settle on the cache
hit; sliver credit and proof budget on the miss path; a sub-chunk stream
on a live chain sends only its closing voucher (mutation-checked).

Closes #2132

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Copilot AI lite review requested due to automatic review settings September 23, 2026 23:28
@thiras thiras self-assigned this Sep 23, 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

🔵 Needs a closer look

The payment-accounting and serve-loop changes span core serving paths and protocol behavior, warranting final human review.

Review effort: Lite
Findings: None

What changed in this PR

Fixes a node liveness vulnerability caused by partially paid chunks being removed from serve queues, which could trigger busy-spinning workers.

Changes:

  • Tracks owed chunk remainders and bounds partial-proof attempts.
  • Adds accounting and no-progress fault checks.
  • Updates tests, ADRs, and changelog documentation.
File Description
crates/​node/​tests/​node_origin_pull.rs Adds pull-through partial-payment regression coverage.
crates/​node/​tests/​client_loopback.rs Adds cache-hit payment and ordering regressions.
crates/​node/​src/​handlers/​client/​wire.rs Updates payment-fault documentation.
crates/​node/​src/​handlers/​client/​voucher.rs Adds owed-chunk and accounting primitives.
crates/​node/​src/​handlers/​client/​serve_leg.rs Applies owed-chunk accounting to pull-through serving.
crates/​node/​src/​handlers/​client/​outcome.rs Removes the obsolete client-abandon outcome.
crates/​node/​src/​handlers/​client/​mod.rs Documents proof-budget semantics.
crates/​node/​src/​handlers/​client/​delivery.rs Fixes cache-hit owed-chunk handling and serve-loop progress.
CHANGELOG.md Records the runtime-wedge fix.
adr/​005-protocol.md Aligns protocol rules with implementation.
adr/​003-payments.md Documents partial-payment and anchoring rules.

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

@github-code-quality

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit acf7e85 in the fix/2132-partial-cre... branch remains at 85%, unchanged from commit 2b3d8e4 in the main branch.

Show a line coverage summary of the most impacted files.
File main 2b3d8e4 fix/2132-partial-cre... acf7e85 +/-
crates/node/src...ent/delivery.rs 94% 94% 0%
crates/client/src/scheduler.rs 95% 95% 0%
crates/protocol/src/framing.rs 97% 97% 0%
crates/node/src...ient/voucher.rs 93% 94% +1%
crates/incentiv...eth_identity.rs 98% 99% +1%
crates/node/src...ient/outcome.rs 94% 95% +1%
crates/node/src...nt/serve_leg.rs 75% 88% +13%

@thiras
thiras merged commit 5ca3e7f into main Sep 23, 2026
29 checks passed
@thiras
thiras deleted the fix/2132-partial-credit-spin branch September 23, 2026 23:39
alpergundogdu added a commit that referenced this pull request Sep 24, 2026
…serve-loop starvation (#2145)

* feat(node): heartbeat the systemd watchdog from the runtime and test serve-loop starvation

A serve loop that goes around without awaiting holds a tokio worker. Once
every worker is held, the node serves nothing, logs nothing and answers no
/metrics scrape, but its systemd unit stays `active`, so nothing restarts it.
#2133 fixed the sliver-credit loop that did this (#2132). This change makes
the failure class recoverable and pins the fixed case.

- `systemd_watchdog`: when systemd passes `WATCHDOG_USEC` and
  `NOTIFY_SOCKET`, a runtime task sends `WATCHDOG=1` every half period. The
  heartbeat runs on the runtime, so it stops when no worker is free, and
  systemd restarts the node. No node setting: the unit's `WatchdogSec=` turns
  it on. Honors `WATCHDOG_PID`; supports path and abstract sockets; the socket
  is non-blocking so a full queue cannot block a worker.
- `a_sliver_voucher_does_not_starve_the_runtime`: runs the #2132 sliver case
  on a one-worker runtime and fails if a heartbeat task on it stops ticking.
  It fails in ~2 s against the parent of #2133 (0 ticks) and passes on main.
  A starved runtime is leaked, not dropped, because the drop would wait
  forever for the worker to yield.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor(node): build the watchdog notifier once and reuse it per heartbeat

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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: deliver loop busy-spins after a partial proof credit and wedges the tokio runtime

2 participants