Merge PR 'Don't fail back holding-cell HTLCs on stale ChannelManager reload' (#5741)
What changed, and why it matters
This patch fixes a bug in the Lightning Dev Kit where reloading an older (stale) copy of the channel manager could incorrectly cancel payments that had actually already moved forward. In Lightning, the ChannelManager and ChannelMonitor are normally kept in sync. If a user accidentally restores an older ChannelManager while the ChannelMonitor is newer, the software force-closes the channel for safety and tries to 'fail backward' any payments it thinks were never sent. The bug was that this fail-backward step also included payments sitting in a temporary 'holding cell' queue. Those payments may have been released and sent to the next node after the older snapshot was taken, so cancelling them would wrongly take money from the user's own pocket or from routing nodes. The fix checks the newer ChannelMonitor: if it shows the payment was committed or even already claimed off-chain, the payment is left for the monitor to resolve instead of being cancelled.
Treat this as a security-relevant correctness fix and include it in the next maintenance release. Node operators who restore ChannelManager state from backups should upgrade before resuming operations. Review backup/restore documentation to warn against running an older ChannelManager against a newer ChannelMonitor.
Security signals we found
Incorrect HTLC fail-backward on stale ChannelManager reload
ChannelMonitor/ChannelManager state desynchronization edge case
Force-close safety path could harm user funds instead of protecting them
Holding-cell HTLCs incorrectly treated as never-committed
Off-chain claim events could be overridden by backwards failure
Evidence from the diff
In channelmanager.rs, during stale-ChannelManager reload handling, shutdown_result.dropped_outbound_htlcs now filters against the current ChannelMonitor. For each dropped outbound HTLC, the code computes its SentHTLCId and checks whether the HTLC still exists in monitor.get_all_current_outbound_htlcs() or has a pending claim event via monitor.pending_claim_event_id(htlc_id). If either is true, the HTLC is skipped instead of being failed backwards with LocalHTLCFailureReason::ChannelClosed. The test file expands the existing stale-manager test into a parameterized do_test_stale_manager_with_since_sent_htlc covering three states: HTLC freed from holding cell, HTLC in a blocked monitor update that was later applied, and HTLC claimed off-chain leaving only a monitor claim event. The new tests assert that in all three cases the HTLC is not failed backwards and the monitor-driven resolution (PaymentSent, PaymentForwarded) occurs.
Changed components
lightning/src/ln/channelmanager.rslightning/src/ln/chanmon_update_fail_tests.rsInspect captured patch +113 / −12
### lightning/src/ln/chanmon_update_fail_tests.rs
@@ -6271,13 +6271,28 @@ fn test_monitor_update_after_funding_spend() {
do_test_monitor_update_after_funding_spend(true);
}
-#[test]
-fn test_stale_manager_with_since_applied_blocked_mon_update() {
+#[derive(Clone, Copy, PartialEq)]
+enum StaleManagerHtlcState {
+ // The HTLC was in the holding cell of the stale `ChannelManager` and has since been committed
+ // to the counterparty.
+ InHoldingCell,
+ // The HTLC was added in a blocked `ChannelMonitorUpdate` of the stale `ChannelManager` and the
+ // update has since been applied to the `ChannelMonitor`.
+ InBlockedMonUpdate,
+ // The HTLC was in the holding cell of the stale `ChannelManager` and has since been claimed by
+ // the counterparty off-chain, leaving only the claim event pending in the `ChannelMonitor`.
+ ClaimEventPending,
+}
+
+fn do_test_stale_manager_with_since_sent_htlc(stale_manager_htlc_state: StaleManagerHtlcState) {
// When a `ChannelManager` is stale compared to a `ChannelMonitor`, it force-closes the channel
// on startup and fails backwards any HTLCs the counterparty was never committed to, including
- // HTLCs added in a blocked `ChannelMonitorUpdate`. A blocked update may have since been
- // applied to the `ChannelMonitor` and its commitment transaction sent to the counterparty, in
- // which case the HTLCs must not be failed backwards.
+ // HTLCs in the holding cell and HTLCs added in a blocked `ChannelMonitorUpdate`. The holding
+ // cell may have since been freed or the blocked update applied to the `ChannelMonitor` and
+ // the commitment transaction sent to the counterparty, in which case the HTLCs must not be
+ // failed backwards. The HTLC may even have since been claimed by the counterparty off-chain,
+ // leaving only the resulting claim event pending in the `ChannelMonitor`, in which case the
+ // claim event must be allowed to drive the HTLC's resolution instead.
let chanmon_cfgs = create_chanmon_cfgs(3);
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
let persister;
@@ -6329,11 +6344,17 @@ fn test_stale_manager_with_since_applied_blocked_mon_update() {
nodes[1].node.process_pending_htlc_forwards();
check_added_monitors(&nodes[1], 0);
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
+ let stale_node_b_ser_holding_cell = nodes[1].node.encode();
nodes[1].node.handle_revoke_and_ack(node_c_id, &cs_raa);
check_added_monitors(&nodes[1], 0);
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
- let stale_node_b_ser = nodes[1].node.encode();
+ let stale_node_b_ser = match stale_manager_htlc_state {
+ StaleManagerHtlcState::InHoldingCell | StaleManagerHtlcState::ClaimEventPending => {
+ stale_node_b_ser_holding_cell
+ },
+ StaleManagerHtlcState::InBlockedMonUpdate => nodes[1].node.encode(),
+ };
// Handling the `PaymentSent` event releases the blocked `ChannelMonitorUpdate`, after which
// payment 2's HTLC is sent to C. Then move the `ChannelMonitor` beyond the stale
@@ -6345,10 +6366,45 @@ fn test_stale_manager_with_since_applied_blocked_mon_update() {
nodes[2].node.handle_update_add_htlc(node_b_id, &bs_add.update_add_htlcs[0]);
nodes[2].node.handle_commitment_signed_batch_test(node_b_id, &bs_add.commitment_signed);
check_added_monitors(&nodes[2], 1);
- let (cs_raa_2, _) = get_revoke_commit_msgs(&nodes[2], &node_b_id);
+ let (cs_raa_2, cs_raa_2_cs) = get_revoke_commit_msgs(&nodes[2], &node_b_id);
nodes[1].node.handle_revoke_and_ack(node_c_id, &cs_raa_2);
check_added_monitors(&nodes[1], 1);
+ if stale_manager_htlc_state == StaleManagerHtlcState::ClaimEventPending {
+ // Settle the HTLC off-chain, leaving its claim event to resolve it after reload.
+ nodes[1].node.handle_commitment_signed_batch_test(node_c_id, &cs_raa_2_cs);
+ check_added_monitors(&nodes[1], 1);
+ let bs_raa_add = get_event_msg!(nodes[1], MessageSendEvent::SendRevokeAndACK, node_c_id);
+ nodes[2].node.handle_revoke_and_ack(node_b_id, &bs_raa_add);
+ check_added_monitors(&nodes[2], 1);
+ expect_and_process_pending_htlcs(&nodes[2], false);
+ expect_payment_claimable!(nodes[2], payment_hash_2, payment_secret_2, 900_000);
+ nodes[2].node.claim_funds(preimage_2, Default::default());
+ check_added_monitors(&nodes[2], 1);
+ expect_payment_claimed!(nodes[2], payment_hash_2, 900_000);
+ let cs_fulfill_2 = get_htlc_update_msgs(&nodes[2], &node_b_id);
+ nodes[1]
+ .node
+ .handle_update_fulfill_htlc(node_c_id, cs_fulfill_2.update_fulfill_htlcs[0].clone());
+ check_added_monitors(&nodes[1], 1);
+ // Drop the upstream claim messages; the monitor must replay the claim after reload.
+ let _ = get_htlc_update_msgs(&nodes[1], &node_a_id);
+ do_commitment_signed_dance(
+ &nodes[1],
+ &nodes[2],
+ &cs_fulfill_2.commitment_signed,
+ false,
+ false,
+ );
+
+ let monitor = get_monitor!(nodes[1], chan_id_bc);
+ assert!(monitor.get_all_current_outbound_htlcs().is_empty());
+ assert!(monitor.list_unacked_monitor_events().iter().any(|(_, event)| {
+ matches!(event, MonitorEvent::HTLCEvent(update)
+ if update.payment_hash == payment_hash_2 && update.payment_preimage == Some(preimage_2))
+ }));
+ }
+
let mon_ab_ser = get_monitor!(nodes[1], chan_id_ab).encode();
let mon_bc_ser = get_monitor!(nodes[1], chan_id_bc).encode();
reload_node!(
@@ -6360,16 +6416,27 @@ fn test_stale_manager_with_since_applied_blocked_mon_update() {
nodes_1_deserialized
);
nodes[1].node.test_process_background_events();
- check_added_monitors(&nodes[1], 1);
-
- // Payment 2's HTLC is left for the `ChannelMonitor` to resolve as C has a commitment
- // transaction including it.
+ let claimed_off_chain = stale_manager_htlc_state == StaleManagerHtlcState::ClaimEventPending;
+ // An off-chain claim also advances the upstream monitor, closing both channels on reload.
+ check_added_monitors(&nodes[1], if claimed_off_chain { 2 } else { 1 });
let events = nodes[1].node.get_and_clear_pending_events();
- assert_eq!(events.len(), 2, "{events:?}");
+ assert_eq!(events.len(), if claimed_off_chain { 4 } else { 2 }, "{events:?}");
assert!(matches!(events[0], Event::PaymentSent { .. }), "{events:?}");
let reason = ClosureReason::OutdatedChannelManager;
assert!(matches!(&events[1], Event::ChannelClosed { reason: r, .. } if *r == reason));
assert!(!nodes[1].node.needs_pending_htlc_processing());
+ if claimed_off_chain {
+ assert!(matches!(&events[2], Event::ChannelClosed { reason: r, .. } if *r == reason));
+ assert!(
+ matches!(events[3], Event::PaymentForwarded { claim_from_onchain_tx: false, .. }),
+ "{events:?}"
+ );
+ check_added_monitors(&nodes[1], 1);
+ return;
+ }
+
+ // Payment 2's HTLC is left for the `ChannelMonitor` to resolve as C has a commitment
+ // transaction including it.
assert!(get_monitor!(nodes[1], chan_id_bc)
.get_all_current_outbound_htlcs()
.values()
@@ -6411,6 +6478,21 @@ fn test_stale_manager_with_since_applied_blocked_mon_update() {
expect_payment_sent(&nodes[0], preimage_2, None, true, true);
}
+#[test]
+fn test_stale_manager_with_since_freed_holding_cell_htlc() {
+ do_test_stale_manager_with_since_sent_htlc(StaleManagerHtlcState::InHoldingCell);
+}
+
+#[test]
+fn test_stale_manager_with_since_applied_blocked_mon_update() {
+ do_test_stale_manager_with_since_sent_htlc(StaleManagerHtlcState::InBlockedMonUpdate);
+}
+
+#[test]
+fn test_stale_manager_with_since_claimed_off_chain_htlc() {
+ do_test_stale_manager_with_since_sent_htlc(StaleManagerHtlcState::ClaimEventPending);
+}
+
#[derive(Clone, Copy, PartialEq)]
enum ForceCloseTrigger {
Holder,
### lightning/src/ln/channelmanager.rs
@@ -20412,7 +20412,26 @@ impl<
},
);
}
+ let monitor_outbound_htlcs = monitor.get_all_current_outbound_htlcs();
for (source, hash, cp_id, chan_id) in shutdown_result.dropped_outbound_htlcs {
+ // HTLCs in the holding cell of the (stale) `ChannelManager` may have since
+ // been freed and sent to the counterparty. If the newer `ChannelMonitor` has
+ // such an HTLC in a counterparty commitment transaction, the counterparty
+ // may yet claim it, so we must leave it to the `ChannelMonitor` to resolve
+ // rather than failing it backwards.
+ let htlc_id = SentHTLCId::from_source(&source);
+ if monitor_outbound_htlcs.contains_key(&source)
+ || monitor.pending_claim_event_id(htlc_id).is_some()
+ {
+ let logger = WithChannelContext::from(
+ &args.logger,
+ &channel.context,
+ Some(hash),
+ );
+ log_info!(logger,
+ "Not failing HTLC which was in the holding cell of the (stale) ChannelManager as the ChannelMonitor will resolve it");
+ continue;
+ }
let reason = LocalHTLCFailureReason::ChannelClosed;
failed_htlcs.push((source, hash, cp_id, chan_id, reason, None));
}Why this scored 66/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.