Repository navigation
Description
What #1230 did, and what it left
#1230 fixed the three watchers that minted a CancellationToken inline and dropped it, by changing their bootstrap signatures to take a token. That makes the mistake hard to repeat — you cannot forget to pass one, and CancellationToken::new() is now greppable to runtime/mod.rs plus payment_settlement's one deliberate self-mint.
But it is a convention, not a guarantee. WatcherConfig.shutdown is already a CancellationToken rather than an Option, so presence was always enforced; the bug was a token nobody cancels, which is a liveness property of the surrounding program. bootstrap(…, CancellationToken::new()) is still spellable at a call site.
Designs that were considered and rejected
Recorded so they aren't re-proposed:
LiveToken(CancellationToken)mintable only from a runtime-owned token. Dies onpayment_settlement: it mints its own and cancels it at a point only it knows (before the channel-close deadline, so a slowcloseChannelcan't starve the flush). "Runtime-owned" is not a constructible predicate. AndLiveToken::new(CancellationToken::new())is still spellable.- Putting the token in
CursorPolicy::Persisted. Looks right; isn't.flush()never consults a token —runflushes on thecfg.shutdownarm. The field would be structurally demanded and never read, and if it held a different token thancfg.shutdownnothing would notice. (Contrastreorg_margin, which node: WatcherConfig::confirmations is wired but never engaged — a reorg guard the code advertises and never applies #1227 did move into that variant, because it genuinely is read from inside the arm. The rule: put data in the variant that reads it.) Teardown::{AbortOnly, Graceful(token)}as aWatcherConfigfield. The bad combination (Persisted+AbortOnly) stays representable without a smart constructor — which is a runtime assert, i.e. a test, not a type. And once node: three watchers construct an inert CancellationToken and throw it away — no graceful shutdown path #1230 landed, all six watchers are graceful, soAbortOnlyhas zero constructors: dead code on arrival.
The design that would actually work
Invert ownership so no call site holds a token at all:
/// Owns the watcher task and its shutdown token. `shutdown()` is the graceful
/// path (flush the cursor, return); `Drop` is the safety net.
pub(crate) struct WatcherHandle {
shutdown: CancellationToken,
_task: AbortOnDrop,
}
impl WatcherHandle {
/// Signal the loop to flush its cursor and return. Idempotent.
pub(crate) fn shutdown(&self) { self.shutdown.cancel(); }
}
/// `WatcherConfig` loses its `shutdown` field; the token is minted here.
pub(crate) fn spawn<P, S>(provider: P, cfg: WatcherConfig, sink: S) -> WatcherHandleNo call site can conjure or drop an inert token, because no call site touches one.
Why it isn't done yet
blacklist_watcher's sink needs the token.BlacklistSink.shutdowndrives the interruptible re-scope. Aspawnthat mints internally would needmake_sink: impl FnOnce(&CancellationToken) -> S. And blacklist doesn't spawn at all — it's apub(crate) async fn runthe runtime awaits — so it needs either an escape hatch (which reintroduces the hole) or a restructure into spawn+handle.- The drop-cancel → abort race means no new guarantee at the margin. That race is precisely why
originalready has a belt-and-braces flush in the runtime and settlement hasflush_checkpoint_on_shutdown. The persisted watchers keep their explicit flushes regardless. - Zero present-day behaviour change. All three sites node: three watchers construct an inert CancellationToken and throw it away — no graceful shutdown path #1230 fixed are non-persisting, so
flushis a no-op for them. The entire payoff is future-proofing: giving one of themCursorPolicy::Persistedlater is what would turn an orphaned token into a silent checkpoint-flush bug.
So it is a 6-site teardown-semantics refactor plus a runtime restructure, for a future-only payoff. Worth doing when someone touches this surface anyway — not worth bolting onto a batch that already rewrote those six files three other ways.
Acceptance
- No
WatcherConfig.shutdownfield;spawnmints and returns aWatcherHandle. blacklist_watcherrestructured onto it (or its sink's token need designed out).payment_settlementkeeps its ordering control over when the cancel fires.- The
run-level flush test from node: three watchers construct an inert CancellationToken and throw it away — no graceful shutdown path #1230 still passes.
Activity
fix(node): thread real shutdown tokens into the three orphaned watche…
fix(node): thread real shutdown tokens into the three orphaned watche…
fix(node): thread real shutdown tokens into the three orphaned watche…
refactor(node): shared ChainProjection<T> wrapper + WatcherHandle shu…