Fix post-restart cleaner crash on replayed fulfill (#3447)
What changed, and why it matters
This commit fixes a bug in the Eclair Lightning node where, after restarting, the node could crash when a peer re-sent payment settlement messages for payments that were already finalized in the database. The crash could stop the node from relaying payment secrets (preimages) upstream, potentially causing payment failures or stuck funds. The fix makes the post-restart cleanup process ignore these duplicate settlements and ensures the cleanup actor is resumed rather than fully restarted if it encounters errors, preserving its state.
Apply the patch and ensure nodes are upgraded before any restart scenario where peers may replay channel updates. Monitor logs for ignored replayed settlements after restarts. Consider additional tests for trampoline payments and multi-part payment edge cases.
Security signals we found
Denial-of-service via post-restart message replay causing actor crash
Loss of preimage relay upstream after cleaner crash
Actor supervision strategy change from restart to resume to preserve state
Database state desynchronization with in-flight HTLC state after restart
Evidence from the diff
The patch modifies PostRestartHtlcCleaner to handle replayed fulfill/fail messages for local payments whose database status is no longer Pending. Previously, receiving a downstream fulfill for an already-final local payment caused an unhandled case and actor failure. The fix adds guards for both fulfill and fail paths: if the outgoing payment is already settled or failed, the message is logged and ignored. Additionally, Relayer’s supervisor strategy is changed to Resume on any child failure instead of the default Restart, preventing the PostRestartHtlcCleaner from losing its initialized state and stashing messages indefinitely.
Changed components
PostRestartHtlcCleanerRelayeroutgoing payment databaseHTLC settlement command handlingInspect captured patch +42 / −2
### eclair-core/src/main/scala/fr/acinq/eclair/payment/relay/PostRestartHtlcCleaner.scala
@@ -184,6 +184,9 @@ class PostRestartHtlcCleaner(nodeParams: NodeParams, register: ActorRef, initial
case Upstream.Local(id) =>
val feesPaid = 0.msat // fees are unknown since we lost the reference to the payment
nodeParams.db.payments.getOutgoingPayment(id) match {
+ case Some(p) if p.status != OutgoingPaymentStatus.Pending =>
+ // The payment may have been settled before the restart while the fulfill wasn't signed yet: our peer then sends it again.
+ log.info("payment {} is already {}, ignoring downstream fulfill", id, p.status.getClass.getSimpleName)
case Some(p) =>
// Note that we lost the onion shared secrets when restarting, so we cannot decrypt the fulfillment payload.
nodeParams.db.payments.updateOutgoingPayment(PaymentSent(p.parentId, paymentPreimage, p.recipientAmount, p.recipientNodeId, PaymentSent.PaymentPart(id, PaymentEvent.OutgoingPayment(fulfilledHtlc.channelId, downstreamNodeId, fulfilledHtlc.amountMsat, settledAt = TimestampMilli.now()), feesPaid, None, startedAt = p.createdAt) :: Nil, None, None, p.createdAt))
@@ -252,7 +255,11 @@ class PostRestartHtlcCleaner(nodeParams: NodeParams, register: ActorRef, initial
case Some(relayedOut) =>
// If this is a local payment, we need to update the DB:
origin.upstream match {
- case Upstream.Local(id) => nodeParams.db.payments.updateOutgoingPayment(PaymentFailed(id, failedHtlc.paymentHash, Nil, startedAt = TimestampMilli.now(), settledAt = TimestampMilli.now()))
+ case Upstream.Local(id) => nodeParams.db.payments.getOutgoingPayment(id) match {
+ case Some(p) if p.status == OutgoingPaymentStatus.Pending => nodeParams.db.payments.updateOutgoingPayment(PaymentFailed(id, failedHtlc.paymentHash, Nil, startedAt = TimestampMilli.now(), settledAt = TimestampMilli.now()))
+ // Already settled before the restart, or never stored (e.g. trampoline payments).
+ case _ => log.info("payment {} has no pending database entry, ignoring downstream failure", id)
+ }
case _ =>
}
val relayedOut1 = relayedOut diff Set((failedHtlc.channelId, failedHtlc.id))
### eclair-core/src/main/scala/fr/acinq/eclair/payment/relay/Relayer.scala
@@ -20,7 +20,7 @@ import akka.Done
import akka.actor.typed.SupervisorStrategy
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.scaladsl.adapter.ClassicActorContextOps
-import akka.actor.{Actor, ActorRef, DiagnosticActorLogging, Props, typed}
+import akka.actor.{Actor, ActorRef, DiagnosticActorLogging, OneForOneStrategy, Props, typed, SupervisorStrategy => ClassicSupervisorStrategy}
import akka.event.Logging.MDC
import akka.event.LoggingAdapter
import fr.acinq.bitcoin.scalacompat.ByteVector32
@@ -58,6 +58,9 @@ class Relayer(nodeParams: NodeParams, router: ActorRef, register: ActorRef, paym
// we pass these to helpers classes so that they have the logging context
implicit def implicitLog: LoggingAdapter = log
+ // The cleaner only receives its Init at startup: restarting it would drop its state and stash every message forever.
+ override val supervisorStrategy: OneForOneStrategy = OneForOneStrategy(loggingEnabled = true) { case _ => ClassicSupervisorStrategy.Resume }
+
private val postRestartCleaner = context.actorOf(PostRestartHtlcCleaner.props(nodeParams, register, initialized), "post-restart-htlc-cleaner")
private val channelRelayer = context.spawn(Behaviors.supervise(ChannelRelayer(nodeParams, register, reputationRecorder_opt)).onFailure(SupervisorStrategy.resume), "channel-relayer")
private val nodeRelayer = context.spawn(Behaviors.supervise(NodeRelayer(nodeParams, register, NodeRelay.SimpleOutgoingPaymentFactory(nodeParams, router, register, reputationRecorder_opt), router)).onFailure(SupervisorStrategy.resume), name = "node-relayer")
### eclair-core/src/test/scala/fr/acinq/eclair/payment/PostRestartHtlcCleanerSpec.scala
@@ -952,6 +952,36 @@ class PostRestartHtlcCleanerSpec extends TestKitBaseClass with FixtureAnyFunSuit
awaitCond(Seq(cmd1) == nodeParams1.db.pendingCommands.listSettlementCommands(channelId_ab_1))
}
+ test("ignore replayed downstream settlement for a local payment that is already final") { f =>
+ import f._
+
+ val local = setupLocalPayments(nodeParams)
+ val relayed = setupChannelRelayedPayments()
+ // Before the restart, our peer fulfilled the first local payment and failed one part of the second one, but those
+ // updates weren't signed yet: the HTLCs are still in our channel data, but the DB already has their final status.
+ val sent = PaymentSent.PaymentPart(local.childIds.head, PaymentEvent.OutgoingPayment(channelId_bc_1, c, 561 msat, settledAt = 10 unixms), 0 msat, None, startedAt = 0 unixms)
+ nodeParams.db.payments.updateOutgoingPayment(PaymentSent(local.childIds.head, preimage1, 561 msat, c, sent :: Nil, None, None, startedAt = 0 unixms))
+ nodeParams.db.payments.updateOutgoingPayment(PaymentFailed(local.childIds(1), paymentHash2, Nil, startedAt = 0 unixms, settledAt = 10 unixms))
+ val (relayer, postRestart) = createRelayer(nodeParams)
+ relayer ! PostRestartHtlcCleaner.Init((local.channel +: relayed.channels).map(_.withChannelKeys(nodeParams)))
+ register.expectNoMessage(100 millis)
+
+ // After reconnection, our peer replays its updates: they must not crash the cleaner.
+ sender.send(relayer, local.fulfills.head)
+ sender.send(relayer, local.fails(1))
+ assert(nodeParams.db.payments.getOutgoingPayment(local.childIds.head).get.status.isInstanceOf[OutgoingPaymentStatus.Succeeded])
+ assert(nodeParams.db.payments.getOutgoingPayment(local.childIds(1)).get.status.isInstanceOf[OutgoingPaymentStatus.Failed])
+ // The replayed HTLCs are settled: the cleaner doesn't wait for them anymore.
+ sender.send(postRestart, PostRestartHtlcCleaner.GetBrokenHtlcs)
+ val relayedOut = sender.expectMsgType[PostRestartHtlcCleaner.BrokenHtlcs].relayedOut
+ assert(!relayedOut.contains(Origin.Cold(Upstream.Local(local.childIds.head))))
+ assert(!relayedOut.contains(Origin.Cold(Upstream.Local(local.childIds(1)))))
+ assert(relayedOut.contains(Origin.Cold(Upstream.Local(local.childIds(2)))))
+
+ // The cleaner still relays preimages for HTLCs we relayed before the restart.
+ sender.send(relayer, buildForwardFulfill(relayed.downstream, relayed.upstream, preimage1))
+ register.expectMsg(Register.Forward(null, relayed.upstream.originChannelId, CMD_FULFILL_HTLC(relayed.upstream.originHtlcId, preimage1, None, None, commit = true)))
+ }
}
object PostRestartHtlcCleanerSpec {Why this scored 55/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.