Skip to content

refactor(node): pair each stop token with its task handle in StopHandle - #1861

Merged
alpergundogdu merged 1 commit into
mainfrom
claude/issue-1853-pr-fuevpb
Aug 27, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
claude/issue-1853-pr-fuevpb

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #1853.

What

The warming-credit aggregator and the receipt writer each stop via a CancellationToken plus a JoinHandle<()> threaded through the runtime structs as independent fields (Infra, ChainHandlers, ShutdownHandles). Awaiting the handle without first cancelling the token hangs until every sink drops — and ClientHandler holds a sink for the whole runtime — so correctness depended on two call sites roughly a hundred lines apart preserving an ordering no type enforced.

This adds crates/node/src/stop_handle.rs with a small StopHandle { shutdown, handle } that makes the pairing structural:

  • StopHandle::spawn(|token| …) mints the token and hands the task the same one it stores, so the pairing holds by construction.
  • shutdown(self) is the only way to join the task, and it always cancels before it awaits — the hang the issue describes is unrepresentable.
  • cancel(&self) keeps the runtime's early signal (right after the router drains) so the tail flush still overlaps settlement/store teardown; the later shutdown().await re-cancels as a no-op.
  • abort_handle(&self) keeps the drain-deadline abort backstop.

Changes

  • spawn_receipt_writer and spawn_warming_creditor no longer take a CancellationToken; they return (sink, StopHandle).
  • The receipt_writer_shutdown/receipt_writer and warming_creditor_shutdown/warming_creditor field pairs in Infra, ChainHandlers, and ShutdownHandles each collapse to a single StopHandle field. The shutdown sequence's ordering, comments, and abort/deadline behavior are unchanged.
  • Unit tests in both modules and the two client_loopback tests that drive the production spawns now use handle.shutdown().await.
  • The tokio-util dev-dep comment in crates/node/Cargo.toml cited the removed spawn_receipt_writer token parameter; it now cites the remaining user (tests/dht_bootstrap.rs).

Validation

  • cargo build, cargo clippy -p decdn-node --all-targets, cargo fmt -- --check all clean.
  • cargo test -p decdn-node --lib: 768 passed.
  • cargo test -p decdn-node --test client_loopback delivery_completes_while_receipt_writer_is_stalled and …a_completed_serve_credits_the_source_through_the_background_aggregator: both pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KieWr2JNMJhgkdm1p2GGT5


Generated by Claude Code

The warming-credit aggregator and the receipt writer each stop via a
CancellationToken plus a JoinHandle threaded through structs as
independent fields, and awaiting the handle without cancelling the token
first hangs until every sink drops — ClientHandler holds one for the
whole runtime. Correctness depended on two call sites ~100 lines apart
preserving an ordering no type enforced.

StopHandle carries the token and the handle as one value: spawn() hands
the task the same token it stores, shutdown() always cancels before it
joins, cancel() keeps the early-signal overlap, and abort_handle() keeps
the drain-deadline backstop. spawn_receipt_writer and
spawn_warming_creditor now mint their own token and return a StopHandle,
so the paired fields in Infra, ChainHandlers, and ShutdownHandles
collapse to one each.

Closes #1853

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KieWr2JNMJhgkdm1p2GGT5
Copilot AI lite review requested due to automatic review settings August 27, 2026 16:14

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 introduces a small StopHandle type in the decdn-node crate to structurally pair a background task’s CancellationToken with its JoinHandle, preventing shutdown hangs caused by awaiting a drain-loop task without cancelling its token first. It refactors the receipt-writer and warming-creditor background tasks and updates the runtime shutdown sequence and tests to use the new unified handle.

Changes:

  • Add StopHandle { shutdown, handle } with spawn, cancel, abort_handle, and shutdown(self).await to enforce cancel-before-join.
  • Refactor spawn_receipt_writer and spawn_warming_creditor to return (sink, StopHandle) instead of taking/returning separate token + join handle pieces.
  • Update runtime shutdown and loopback/unit tests to use StopHandle::cancel() for early drain signaling and StopHandle::shutdown().await for the final join.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/node/tests/client_loopback.rs Updates integration tests to stop/join background receipt writer and warming creditor via StopHandle::shutdown().await.
crates/node/src/warming_allowance.rs Refactors warming-credit aggregator spawn API to return StopHandle and updates unit tests accordingly.
crates/node/src/stop_handle.rs Adds the new StopHandle abstraction that structurally enforces cancel-before-join for drain-loop tasks.
crates/node/src/runtime/mod.rs Collapses separate shutdown token + join handle fields into StopHandle and preserves shutdown ordering with safe stop/join semantics.
crates/node/src/receipt_log.rs Refactors receipt writer spawn API to return StopHandle and updates related unit tests and docs.
crates/node/src/lib.rs Exposes the new stop_handle module.
crates/node/Cargo.toml Updates the tokio-util dev-dependency comment to reflect the remaining direct CancellationToken usage.

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

Copy link
Copy Markdown
Contributor Author

The test check failed on node_origin_pull::silent_upstreams_do_not_starve_the_candidate_loop (fail-fast stopped 224 remaining tests). Assessment: not this PR's failure.

No fix to port exists yet (nothing open addresses this specific test's budget). Re-running the failed job once to confirm; a second failure I'll treat as real.


Generated by Claude Code

@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.04% (was 82.08%, ▼ 0.04%) — 67998/82882 lines

File Coverage Lines Δ
crates/node/src/receipt_log.rs 90.86% 477/525 ▼ 0.20%
crates/node/src/runtime/mod.rs 47.80% 1154/2414 ▼ 0.02%
crates/node/src/stop_handle.rs 68.42% 13/19 new
crates/node/src/warming_allowance.rs 98.03% 298/304 ▼ 0.07%
History
Commit Total Δ vs base
0c8b17e (HEAD) 82.04% ▼ 0.04%

@alpergundogdu
alpergundogdu merged commit 56d77fb into main Aug 27, 2026
51 of 53 checks passed
@alpergundogdu
alpergundogdu deleted the claude/issue-1853-pr-fuevpb branch August 27, 2026 19:13
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: pair shutdown CancellationToken with its JoinHandle in one type

3 participants