← Back to Pull requests

peer+chancloser: buffer RBF close bumps in FSM and tear down on CloseFin #11575

Merged
Roasbeef merged 8 files (1962 lines) on Nov 23, 2026 21:29 UTC
View on GitHub
Roasbeef Roasbeef commented on Nov 13, 2026 03:03 UTC
Author

Change Description

In this PR, we resolve #11571 and #11488 by moving RBF cooperative close bump serialization and buffering directly into the RbfChanCloser state machine and unifying CloseFin channel+link teardown under a single per-closer lifecycle observer.

Previously, startRbfChanCloser inspected CurrentState() out of band and branched on it: bumps arriving in ShutdownPending or ChannelFlushing fell through to a warning log and were silently dropped, while bumps in ClosingNegotiation blocked the peer's channelManager goroutine inside waitUntilRbfCoastClear using the peer's lifetime context rather than the caller's, stalling other peer operations (and hanging forever in LocalCloseStart, CloseErr, or LocalOfferSent without a peer reply).

We eliminate waitUntilRbfCoastClear and the CurrentState() switch entirely. Instead, startRbfChanCloser sends a single non-blocking LocalCloseReq event into the state machine, which every state handles deterministically:

  • In ShutdownPending and ChannelFlushing, an incoming bump is buffered in PendingBump and applied as soon as ChannelFlushed fires (transitioning into CloseErr if an explicit bump exceeds local balance, while preserving LocalCloseStart when an unpayable initial close waits for the remote party).

  • In LocalOfferSent, a bump is buffered in PendingOffer and emitted as an internal SendOfferEvent right after LocalSigReceived completes the active round and updates RemoteCloseeNonce for taproot channels.

  • In LocalCloseStart, ClosePending, and CloseErr, LocalCloseReq triggers a new offer right away.
  • If an RPC caller cancels before a buffered bump starts, observeRbfCloseUpdatesWithSeq emits a sequence-tagged CancelPendingOffer so only that unstarted request is retracted. When maybeNotifyTxBroadcast reports a CloseErr, the observer exits right away instead of lingering until context cancellation. On fatal FSM errors, chanErrorReporter.ReportError retires the failed closer from activeChanCloses and aborts waiting callers in activeCloseReqs with the FSM error before re-initializing, and startRbfChanCloser checks closer pointer identity and liveness (isSameLiveRbfCloser) so a caller cannot bind to a stopped or replaced closer.

    For #11488, we add Done() on protofsm.StateMachine and generalize chanFlushEventSentinel into rbfCloserLifecycleSentinel, registered before the state machine starts in initRbfChanCloser. Once CloseFin is reached (whether locally initiated, remote initiated, or restored), the sentinel retires the closer from activeChanCloses, delivers success to any waiting RPC callers, and calls WipeChannel to remove the channel from activeChannels and stop its link in the switch.

    Steps to Test

    go test -race -count=1 ./protofsm/... ./lnwallet/chancloser/... ./peer/...
    make itest icase="rbf coop close"
    make itest-only icase="coop close rbf with reorg"
    

    Fixes #11571
    Fixes #11488

    Diff: 1962 lines (+1538 / -424) across 8 files Reviewer: litbot-9000 Review duration: 15505m
    All Vouch review confidence checks passed
    Residual risk 0.14 = Change Risk (0.53) × (1 − Review Confidence (0.74)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 1962 lines diff across 8 files
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.49 · 1 substantive comment(s)
    0.49
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    74%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Real PR Conversation Timeline
    4 items
    litbot-9000 litbot-9000 approved these changes on Nov 13, 2026 21:29 UTC
    Approved Reviewer

    /gateway review

    Approve. This is a substantial FSM/concurrency change and it holds up under close reading. The two core problems are genuinely gone, by construction rather than by patch-over:

  • No more channelManager stall: waitUntilRbfCoastClear and the out-of-band CurrentState() switch are deleted outright. LocalCloseReq is now a first-class event valid in every state, so a bump in ShutdownPending/ChannelFlushing buffers as a Seq-tagged PendingBump (applied on ChannelFlushed, preferred over the ideal fee rate), a bump in LocalOfferSent buffers as PendingOffer (emitted as an internal SendOfferEvent right after LocalSigReceived completes the round), and LocalCloseStart/ClosePending/CloseErr act immediately.

  • Unified CloseFin teardown: the lifecycle sentinel retires the closer from activeChanCloses, delivers success, then WipeChannels — in that order, so a concurrent caller can't register after takeAll and strand. The new protofsm.StateMachine.Done() plus p.cg.Done() give the sentinel clean exits, and the flush hook is registered with the peer-lifetime context (consistent with the hook lifetime semantics from #11483).
  • Edges I checked specifically: taproot RemoteCloseeNonce is updated from the completing round's ClosingSig before a buffered offer is emitted, so the next offer uses the fresh nonce; CancelPendingOffer retracts only a Seq-matching unstarted bump; ReportError captures the chanPoint and aborts waiting callers with the FSM error before re-initializing, and isSameLiveRbfCloser prevents binding to a stopped/replaced closer.

    Verified by execution: go test -race -count=1 ./protofsm/... ./lnwallet/chancloser/... ./peer/... — all green with the race detector. The nine new tests map to the PR's claims (buffering across states, restored-shutdown bump, channelManager staying free, caller-cancel retraction, CloseFin teardown, CloseErr no-leak, ReportError answering callers, identity guard). Mutation check: disabling the PendingBump buffering fails TestRbfLocalCloseReqBuffering, so the coverage bites. gofmt/vet clean; the long lines are all nolint:ll-approved. Release note present.

    Not verified: itests (no Docker here) — the two make itest cases in the PR's test plan are CI-covered.

    Note: the failing "CI result" check is the ci-hold label on this PR, not the code.

    — claudell ⚡ (OpenClaw dev agent; approval posts as litbot-9000 and does not count toward branch protection)

    Amirhosein Amirhosein commented on Nov 14, 2026 06:14 UTC
    Reviewer

    Walked through the FSM transitions and peer lifecycle changes and ran go test -race -count=1 ./protofsm/... ./lnwallet/chancloser/... ./peer/... locally. The in-FSM buffering and CloseFin sentinel cleanup look really clean.

    A few small questions on peer/brontide.go in case these edge cases matter:

  • startRbfChanCloser (peer/brontide.go:5069-5091): I see coopCloseStates := rbfCloser.RegisterStateEvents() (L5050) is registered before SendEvent so fast transitions aren't missed, which makes sense. If req.Ctx is already cancelled when startRbfChanCloser runs, could spawning observeRbfCloseUpdatesWithSeq (L5072) before rbfCloser.SendEvent(sendCtx, &chancloser.LocalCloseReq{..., Seq: bumpSeq}) (L5087) let the observer hit <-closeReq.Done() and send CancelPendingOffer{Seq: bumpSeq} before SendEvent runs? Since StateMachine.SendEvent selects between s.events <- event (buffered size 1) and <-ctx.Done(), it looks like SendEvent could still enqueue LocalCloseReq after CancelPendingOffer was already processed as a no-op.

  • chanErrorReporter.ReportError (peer/brontide.go:4606-4646): Pulling activeChanCloses.LoadAndDelete(c.chanID) (L4607) up before abortRemainingCloseReqs (L4608) makes sense so concurrent callers can't bind to the failing closer while it's being retired. Since c.chanID is already removed from activeChanCloses at L4607, if Disconnect() runs concurrently between LoadAndDelete and initRbfChanCloser(lnChan) (L4646), abandonChanClosers won't see this channel, so initRbfChanCloser could still start a new closer and call p.cg.WgAdd(1) after p.cg.Quit(). Would it be worth guarding the re-init on c.peer.cg.Done()?

  • observeRbfCloseUpdatesWithSeq (peer/brontide.go:4499-4505): I saw TestRbfCloseErrDoesNotLeakOnRetry covers exiting the first observer on CloseErr (localDone) so its later cancellation doesn't re-emit ClosingNegotiation and leak CloseErr to a retry caller. Because ClosingNegotiation is a dual state and Local stays in *chancloser.CloseErr until a new LocalCloseReq arrives, what happens if a remote OfferReceivedEvent arrives after caller 2 registers coopCloseStates but before caller 2's LocalCloseReq is processed? It looks like that transition would still carry Local: CloseErr and trigger localDone on caller 2's observer (or on an earlier caller waiting for a broadcast ClosePending tx to confirm if a second caller's bump hits CloseErr). Not sure if that's expected in practice or if CloseErr should only be consumed once.
  • litbot-9000 litbot-9000 commented on Nov 23, 2026 00:50 UTC
    Contributor

    @roasbeef, remember to re-request review from reviewers when ready

    Reviewers
    Assignees
    Roasbeef Roasbeef
    Labels
    risk: low confidence: 74.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 53.2% (MEDIUM) · Higher = larger blast radius
    53.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 74.4% · Lower = rushed or shallow review
    74.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 13.6% · Safe to merge
    13.6%