peer+chancloser: buffer RBF close bumps in FSM and tear down on CloseFin #11575
/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:
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.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)
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.@roasbeef, remember to re-request review from reviewers when ready
litbot-9000
Roasbeef
Change Description
In this PR, we resolve #11571 and #11488 by moving RBF cooperative close bump serialization and buffering directly into the
RbfChanCloserstate machine and unifyingCloseFinchannel+link teardown under a single per-closer lifecycle observer.Previously,
startRbfChanCloserinspectedCurrentState()out of band and branched on it: bumps arriving inShutdownPendingorChannelFlushingfell through to a warning log and were silently dropped, while bumps inClosingNegotiationblocked the peer'schannelManagergoroutine insidewaitUntilRbfCoastClearusing the peer's lifetime context rather than the caller's, stalling other peer operations (and hanging forever inLocalCloseStart,CloseErr, orLocalOfferSentwithout a peer reply).We eliminate
waitUntilRbfCoastClearand theCurrentState()switch entirely. Instead,startRbfChanClosersends a single non-blockingLocalCloseReqevent into the state machine, which every state handles deterministically:ShutdownPendingandChannelFlushing, an incoming bump is buffered inPendingBumpand applied as soon asChannelFlushedfires (transitioning intoCloseErrif an explicit bump exceeds local balance, while preservingLocalCloseStartwhen an unpayable initial close waits for the remote party).LocalOfferSent, a bump is buffered inPendingOfferand emitted as an internalSendOfferEventright afterLocalSigReceivedcompletes the active round and updatesRemoteCloseeNoncefor taproot channels.LocalCloseStart,ClosePending, andCloseErr,LocalCloseReqtriggers a new offer right away.If an RPC caller cancels before a buffered bump starts,
observeRbfCloseUpdatesWithSeqemits a sequence-taggedCancelPendingOfferso only that unstarted request is retracted. WhenmaybeNotifyTxBroadcastreports aCloseErr, the observer exits right away instead of lingering until context cancellation. On fatal FSM errors,chanErrorReporter.ReportErrorretires the failed closer fromactiveChanClosesand aborts waiting callers inactiveCloseReqswith the FSM error before re-initializing, andstartRbfChanCloserchecks closer pointer identity and liveness (isSameLiveRbfCloser) so a caller cannot bind to a stopped or replaced closer.For #11488, we add
Done()onprotofsm.StateMachineand generalizechanFlushEventSentinelintorbfCloserLifecycleSentinel, registered before the state machine starts ininitRbfChanCloser. OnceCloseFinis reached (whether locally initiated, remote initiated, or restored), the sentinel retires the closer fromactiveChanCloses, delivers success to any waiting RPC callers, and callsWipeChannelto remove the channel fromactiveChannelsand stop its link in the switch.Steps to Test
Fixes #11571
Fixes #11488