Repository navigation
refactor(node): pair each stop token with its task handle in StopHandle - #1861
Conversation
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
There was a problem hiding this comment.
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 }withspawn,cancel,abort_handle, andshutdown(self).awaitto enforce cancel-before-join. - Refactor
spawn_receipt_writerandspawn_warming_creditorto 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 andStopHandle::shutdown().awaitfor 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.
|
The
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 |
Coverage reportTotal: 82.04% (was 82.08%, ▼ 0.04%) — 67998/82882 lines
History
|
Closes #1853.
What
The warming-credit aggregator and the receipt writer each stop via a
CancellationTokenplus aJoinHandle<()>threaded through the runtime structs as independent fields (Infra,ChainHandlers,ShutdownHandles). Awaiting the handle without first cancelling the token hangs until every sink drops — andClientHandlerholds 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.rswith a smallStopHandle { 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 latershutdown().awaitre-cancels as a no-op.abort_handle(&self)keeps the drain-deadline abort backstop.Changes
spawn_receipt_writerandspawn_warming_creditorno longer take aCancellationToken; they return(sink, StopHandle).receipt_writer_shutdown/receipt_writerandwarming_creditor_shutdown/warming_creditorfield pairs inInfra,ChainHandlers, andShutdownHandleseach collapse to a singleStopHandlefield. The shutdown sequence's ordering, comments, and abort/deadline behavior are unchanged.client_loopbacktests that drive the production spawns now usehandle.shutdown().await.tokio-utildev-dep comment incrates/node/Cargo.tomlcited the removedspawn_receipt_writertoken parameter; it now cites the remaining user (tests/dht_bootstrap.rs).Validation
cargo build,cargo clippy -p decdn-node --all-targets,cargo fmt -- --checkall clean.cargo test -p decdn-node --lib: 768 passed.cargo test -p decdn-node --test client_loopback delivery_completes_while_receipt_writer_is_stalledand…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