Skip to content

node: make the watcher shutdown-token invariant unrepresentable (WatcherHandle ownership inversion) #1236

Closed

Description

@thiras

Follow-up from #1230 / #1110.

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 on payment_settlement: it mints its own and cancels it at a point only it knows (before the channel-close deadline, so a slow closeChannel can't starve the flush). "Runtime-owned" is not a constructible predicate. And LiveToken::new(CancellationToken::new()) is still spellable.
  • Putting the token in CursorPolicy::Persisted. Looks right; isn't. flush() never consults a token — run flushes on the cfg.shutdown arm. The field would be structurally demanded and never read, and if it held a different token than cfg.shutdown nothing would notice. (Contrast reorg_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 a WatcherConfig field. 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, so AbortOnly has 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) -> WatcherHandle

No call site can conjure or drop an inert token, because no call site touches one.

Why it isn't done yet

  1. blacklist_watcher's sink needs the token. BlacklistSink.shutdown drives the interruptible re-scope. A spawn that mints internally would need make_sink: impl FnOnce(&CancellationToken) -> S. And blacklist doesn't spawn at all — it's a pub(crate) async fn run the runtime awaits — so it needs either an escape hatch (which reintroduces the hole) or a restructure into spawn+handle.
  2. The drop-cancel → abort race means no new guarantee at the margin. That race is precisely why origin already has a belt-and-braces flush in the runtime and settlement has flush_checkpoint_on_shutdown. The persisted watchers keep their explicit flushes regardless.
  3. 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 flush is a no-op for them. The entire payoff is future-proofing: giving one of them CursorPolicy::Persisted later 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

Metadata

Metadata

Assignees

Labels

prio:lowrust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions