Skip to content

fix(node): stop a stream whose pool closes mid-stream - #2812

Merged
yigitdot merged 7 commits into
mainfrom
claude/stream-closed-pool-termination-d7dadd
Nov 1, 2026
Merged

yigitdot merged 7 commits into
mainfrom
claude/stream-closed-pool-termination-d7dadd

Conversation

@yigitdot

@yigitdot yigitdot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2805.

Problem

The mid-stream pool re-check in both serve loops (deliver, serve_leg) kept serving bytes the node cannot redeem:

  • Closed pool. The view forgets a reclaimed or Closed pool, so cached_status returned None and the re-check failed open (fix(node): count a skipped mid-stream pool re-check and test the serve-miss PoolExhausted stop #2789 only counted it). redeemMany reverts PoolClosed. On a serve miss the node also kept paying its upstream.
  • Forgotten signer. signer_spent_cached read 0 for a forgotten pool, so the signer cap re-check passed too.
  • Closing past its deadline. Neither loop read status.lifecycle.
  • Slow batches. The re-check ran only after a whole recoup batch. A slow payer could hold a batch open across many proof waits while the pool closed, and on a serve miss each credited proof lifted the pull leg paid frontier.

Change

  • ClientHandler::pool_recheck_stop replaces pool_view_status_cached. With a pool view wired it stops on:
    • no status for the pool → ServeStop::PoolClosed;
    • Closing { deadline } inside the capability expiry margin (one redeem interval + REDEEM_LANDING_SLACK_SECS, via inside_expiry_margin) → ServeStop::PoolClosed. The margin leaves the redeemer one interval to sweep the last voucher;
    • a drained pool → ServeStop::PoolExhausted, as before.
      With no view wired it still fails open.
  • Re-check between proofs. Both loops also run pool_recheck_stop between the proofs of one batch, once that batch outlasts one re-check interval. The clock is per batch and the check waits for the first proof, so a batch that settles inside one interval never reads the pool there.
  • Wire reason: PoolExhausted for both stops. No wire change: clients already treat it as a funding stop that keeps accepted proofs, and keep an unread proof that a later proof on the lane covers (refuses_proof). Recovery for a closed pool is a new pool (topUp reverts PoolNotOpen).
  • Signer case: the signer re-check runs only after the pool re-check passes, so the view still holds the pool and spent is real.
  • Metric: decdn_serve_stream_midstream_pool_closed_total replaces decdn_serve_pool_recheck_skipped_total and joins INBOUND_FAILURE_REASONS. The skip-warn throttle is gone: each stream stops once.
  • Docs: ADR 003 gets a new "Mid-stream pool close re-check" paragraph, the between-proofs re-check in its cadence paragraph, and an extended PoolExhausted reject row. ADR 005, the observability appendix and the PoolExhausted docstring are updated.

Tests

  • Unit: forgotten pool → PoolClosed; no view → None; open + funded → None; Closing at its deadline → PoolClosed; one second inside / outside the margin; drained open pool → PoolExhausted; Closing and drained → PoolClosed wins.
  • Loopback, in both the cache-hit and the serve-miss loops: a forgotten pool and a Closing pool past its cutoff each stop in-band with PoolExhausted, finish with a FIN, and count as pool_closed, not pool_exhausted. A pool that turns Closing a day before its deadline serves to StreamEnd.
  • Forgotten signer (spent = 0, the production projection read): stops as pool_closed, never as signer_cap_exhausted.
  • Slow payer, in both loops: the payer pays a sliver after the close cutoff and holds the rest; the stream stops between proofs. The old loops park on the next proof and time out.
  • Stop between the root voucher and its first reveal: the client keeps the unread reveal (ledger ahead of the node claim), and a resumed fetch on the same ledger completes with no rebase and no rejection. Fails against the old loops.
  • a_mid_stream_pool_exhausted_keeps_the_proofs_the_node_accepted now drains after two paid intervals, so it still stops after a credited proof; the earlier drain point is the new root/reveal test.
  • cargo nextest run -p decdn-node: 1905 passed at fd6c529; the root/reveal test passed 5 of 5 runs. Workspace clippy --all-targets -D warnings, fmt and cargo doc with warnings denied are clean.

Follow-ups

🤖 Generated with Claude Code

The mid-stream pool re-check in both serve loops failed open when the pool
view no longer held the pool, and never read the pool's lifecycle. A stream
kept running on a closed pool, or on a Closing pool past its dispute
deadline, where redeemMany reverts PoolClosed and the node is not paid.

pool_recheck_stop now stops the stream in-band with PoolExhausted when the
view holds no status for the pool, or when the pool is Closing and its
deadline falls inside the capability expiry margin (one redeem interval plus
the landing slack). The stop is ServeStop::PoolClosed and counts on
decdn_serve_stream_midstream_pool_closed_total, which replaces
decdn_serve_pool_recheck_skipped_total. With no pool view wired the check
still fails open. The signer cap re-check runs only after the pool re-check
passes, so it never reads a forgotten pool's zero spend.

Closes #2805

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 1, 2026 05:54

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@github-code-quality

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

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 034f8a7 claude/stream-closed... 9ca79b3 +/-
77% 77% 0%
View coverage of the 7 most impacted files.
File main 034f8a7 claude/stream-closed... 9ca79b3 +/-
crates/client/s...undle/engine.rs 65% 65% 0%
crates/node/src...s/client/mod.rs 94% 94% 0%
crates/node/src...ent/delivery.rs 95% 95% 0%
crates/cli/src/.../bundle_pull.rs 63% 63% 0%
crates/node/src...nt/serve_leg.rs 92% 92% 0%
crates/node/src...ient/outcome.rs 99% 99% 0%
crates/client/src/lib.rs 89% 90% +1%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 01, 2026 22:16 UTC

Three comments still said a pool missing from the projection makes the
serve gate fail open; a forgotten pool now refuses admission and stops any
live stream as PoolClosed, so say that. The pool-closed counter docs
claimed redeemMany reverts on a Closing pool inside the margin; it reverts
only past the deadline — the margin stop runs early so the last sweep can
land. Bind "redeem margin" to its definition, restore the repaid-pool
remaining > M clause in the loop comments, note the reorg self-heal on
pool_recheck_stop, and drop a dangling "Both".

Tests: pin the close-over-drain precedence (a Closing pool at its deadline
that is also drained stops as PoolClosed), drive the PoolExhausted branch
of pool_recheck_stop directly, and assert the drained-pool loopback stops
count zero on the pool-closed counter.

Co-Authored-By: Claude Fable 5 <[email protected]>
@yigitdot

yigitdot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

Ran the four-agent review (code, tests, comments, silent failures) on e7ec33a: no critical findings. 3a99069 addresses the rest — three stale fail-open comments, the redeemMany-reverts overstatement for a Closing pool inside the margin, a close-over-drain precedence unit test plus a direct PoolExhausted-branch test, counter-separation asserts in the drained-pool loopback tests, a reorg self-heal note, and wording nits. The one real out-of-scope finding (the redeemer has no close-deadline wake, so the margin's last sweep is a window, not a guarantee) is filed separately.

yigitdot and others added 2 commits November 1, 2026 21:46
…pool-termination-d7dadd

# Conflicts:
#	adr/003-payments.md
A forgotten pool's signer reads spent = 0 in the production projection,
so the signer re-check alone would keep serving. The new loopback test
pins that the pool re-check stops the stream first, as a pool close.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yigitdot
yigitdot requested a review from thiras November 1, 2026 18:55
@thiras
thiras requested a balanced review from Copilot November 1, 2026 19:56

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.

🟡 Changes recommended

Proof cutoffs are checked only after collection batches, allowing delayed proofs to advance claims past closing or dispute deadlines and potentially miss the final redeemable sweep.

1 open issue

Review effort: Balanced

Comment on lines +612 to 614
if let Some(stop) = self.pool_recheck_stop(&lane_key, &hash).await {
self.write_reject(send, VoucherRejectReason::PoolExhausted, None)
.await?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Partly fixed in fd6c529; the rest is out of scope or by design.

  • Check only after the batch. Fixed. Both loops now also run pool_recheck_stop between proofs, once a batch outlasts one re-check interval (per-batch clock, so a fast batch never reads the pool there). In serve_leg this also stops a slow batch from lifting the pull leg's paid frontier. New tests closing_pool_stops_a_slow_payers_{cache_hit,serve_miss}_batch_between_proofs: the payer sends a sliver after the cutoff and holds the rest; the old loops park on the next proof, the new ones stop.
  • Claims past the deadline. No downstream bytes are at stake: a batch only collects proofs for chunks already sent (pop_payable), and the re-check runs before the next delivery. Refusing a proof for bytes already sent does not recover them.
  • Missed last sweep when redeem hints drop. Real, but a redeemer defect: it has a capability-expiry cutoff wake (lane_cutoff_wake) and no pool close-deadline wake. Tracked in redeemer: no close-deadline wake, so the pool-close margin's last sweep can miss #2813.
  • Clock/check delay in the margin. The close cutoff uses the same coarse clock and the same margin (redeem interval + landing slack) as the capability-expiry gate in both proof paths. Changing it is a design change for both gates, not this PR.

yigitdot and others added 2 commits November 1, 2026 23:55
Both serve loops ran the mid-stream pool re-check only after a whole
recoup batch. A slow payer could hold a batch open across many proof
waits while the pool closed or crossed its redeem margin, and on a serve
miss each credited proof lifted the pull leg's paid frontier. The
re-check now also runs between proofs, once a batch outlasts one
re-check interval.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The re-check between proofs can stop a stream after the epoch's root
voucher and before the node reads the reveal the client already sent.
The client keeps that reveal, so its ledger runs ahead of the node's
claim. The new loopback test pins that a resumed fetch on the same
ledger completes with no rebase and no rejection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pool-termination-d7dadd

# Conflicts:
#	adr/appendix-observability.md
#	crates/node/src/handlers/client/mod.rs
#	crates/node/src/handlers/client/tests.rs
#	crates/node/src/metrics.rs
#	crates/node/src/metrics/tests.rs
#	crates/node/src/pool_view.rs
@yigitdot
yigitdot merged commit 68f4884 into main Nov 1, 2026
67 of 69 checks passed
@yigitdot
yigitdot deleted the claude/stream-closed-pool-termination-d7dadd branch November 1, 2026 22:20
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: stop a stream whose pool closes mid-stream

3 participants