Merge PR 'Track refused monitor updates as pending until persisted' (#5663)
What changed, and why it matters
This commit fixes a bug in the Lightning Dev Kit's handling of channel monitor updates that are refused because the channel's funding transaction was already spent on-chain. Previously, such refused updates were persisted as a full monitor snapshot rather than as a normal update, and were not tracked as 'pending' while asynchronous persistence was in progress. That meant a later update could be marked complete before the refused update (which might contain important payment data like a forwarded HTLC preimage) was actually durable on disk. The fix makes refused updates follow the same persistence and pending-tracking path as accepted updates, and also allows monitor restoration to continue even if replaying a stored update is refused.
Treat this as a bug-fix commit with security relevance for nodes using asynchronous ChannelMonitor persistence. Users running LDK-based nodes should upgrade to a release containing this fix to avoid potential state inconsistency after crashes or restarts involving refused monitor updates. No immediate emergency response is indicated, but operators should monitor for releases and apply updates during normal maintenance windows.
Security signals we found
Durability ordering bug: later MonitorEvent::Completed could imply earlier refused update was persisted
Refused monitor update may carry forwarded HTLC preimage, creating risk of inconsistent state across restart
Asynchronous persistence tracking gap for refused updates
Monitor restoration failure mode when replaying refused updates
Evidence from the diff
The change is in rust-lightning’s ChainMonitor and persistence code. When a ChannelMonitorUpdate is applied in-memory but returns Err (typically because no_further_updates_allowed is true after the funding output is spent), the code now persists it via update_persisted_channel with Some(update) rather than falling back to a full monitor write with None. It also always adds the update_id to pending_monitor_updates when persistence is InProgress, so completion of later updates does not imply this refused update is durable. In persist.rs, during monitor restoration, a refused replayed update is now logged and skipped instead of aborting the entire restore. The test chanmon_update_fail_tests.rs is updated to assert that PaymentClaimed does not fire until the refused update’s persist is also marked complete.
Changed components
lightning/src/chain/chainmonitor.rslightning/src/util/persist.rslightning/src/ln/chanmon_update_fail_tests.rsInspect captured patch +56 / −35
### lightning/src/chain/chainmonitor.rs
@@ -1236,32 +1236,22 @@ where
);
let update_id = update.update_id;
- let persist_res = if update_res.is_err() {
- // Even if updating the monitor returns an error, the monitor's state will
- // still be changed. Therefore, we should persist the updated monitor despite the error.
- // We don't want to persist a `monitor_update` which results in a failure to apply later
- // while reading `channel_monitor` with updates from storage. Instead, we should persist
- // the entire `channel_monitor` here.
- log_warn!(logger, "Failed to update ChannelMonitor. Going ahead and persisting the entire ChannelMonitor");
- self.persister.update_persisted_channel(
- monitor.persistence_key(),
- None,
- monitor,
- )
- } else {
- self.persister.update_persisted_channel(
- monitor.persistence_key(),
- Some(update),
- monitor,
- )
- };
+ // The update may have been `Err`ed because the channel is closed, but it was
+ // still applied to the in-memory monitor, so persist it like any other update.
+ if update_res.is_err() {
+ log_info!(
+ logger,
+ "Failed to update ChannelMonitor, likely due to the channel being closed already"
+ );
+ }
+ let persist_res = self.persister.update_persisted_channel(
+ monitor.persistence_key(),
+ Some(update),
+ monitor,
+ );
match persist_res {
ChannelMonitorUpdateStatus::InProgress => {
- // If the update was refused by the `ChannelMonitor`, then it was not individually
- // persisted and it will never be marked as completed via `channel_monitor_updated`.
- if update_res.is_ok() {
- pending_monitor_updates.push(update_id);
- }
+ pending_monitor_updates.push(update_id);
log_debug!(
logger,
"Persistence of ChannelMonitorUpdate id {:?} in progress",
### lightning/src/ln/chanmon_update_fail_tests.rs
@@ -5901,10 +5901,9 @@ fn do_test_monitor_update_after_funding_spend(async_persist: bool) {
// (like PaymentClaimed) must still fire.
//
// With `async_persist`, the persister returns InProgress for every update and the test
- // marks them complete via `channel_monitor_updated`, except for the full-monitor persist
- // of the refused stale commitment update. Persists without a `ChannelMonitorUpdate` are
- // never marked complete (see `Persist::update_persisted_channel`), so it must not hold up
- // the completion of the updates that follow it.
+ // marks them complete via `channel_monitor_updated`, including the persist of the refused
+ // stale commitment update. As the refused update still changed the monitor's state, its
+ // persist must hold up the completion of the updates that follow it.
let chanmon_cfgs = create_chanmon_cfgs(2);
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
@@ -5954,13 +5953,14 @@ fn do_test_monitor_update_after_funding_spend(async_persist: bool) {
nodes[1].node.handle_update_add_htlc(node_a_id, &payment_event.msgs[0]);
// B processes commitment_signed. The monitor applies the update but returns Err
- // because no_further_updates_allowed() is true, so the full monitor is persisted
- // instead of the update. ChainMonitor returns InProgress, freezing the channel.
+ // because no_further_updates_allowed() is true. The update is persisted anyway, and
+ // ChainMonitor returns InProgress, freezing the channel.
if async_persist {
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
}
nodes[1].node.handle_commitment_signed(node_a_id, &payment_event.commitment_msg[0]);
check_added_monitors(&nodes[1], 1);
+ let refused_update_id = get_monitor!(nodes[1], chan_id).get_latest_update_id();
// B claims payment 1. The preimage monitor update also returns InProgress (deferred),
// so no Completed-while-InProgress assertion fires.
@@ -5991,6 +5991,15 @@ fn do_test_monitor_update_after_funding_spend(async_persist: bool) {
if async_persist {
let update_id = get_monitor!(nodes[1], chan_id).get_latest_update_id();
nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(chan_id, update_id).unwrap();
+ // The ChannelForceClosed update is now marked completed, but the refused update's
+ // persist is still in progress, so its completion action (PaymentClaimed) must not
+ // fire yet.
+ assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
+ nodes[1]
+ .chain_monitor
+ .chain_monitor
+ .channel_monitor_updated(chan_id, refused_update_id)
+ .unwrap();
}
// Second event cycle: the ChannelForceClosed completion resolves, unblocking the
### lightning/src/util/persist.rs
@@ -1673,18 +1673,29 @@ impl<
}
for (update_name, update_res) in MultiResultFuturePoller::new(update_futures).await {
let update = update_res?;
- monitor
- .update_monitor(&update, &self.broadcaster, &self.fee_estimator, &self.logger)
- .map_err(|e| {
+ if let Err(e) = monitor.update_monitor(
+ &update,
+ &self.broadcaster,
+ &self.fee_estimator,
+ &self.logger,
+ ) {
+ // Updates the monitor refused (e.g. because the funding output was already spent
+ // on-chain) are persisted anyway, so replaying them here may fail for the same
+ // reason the live monitor refused them -- or succeed, if the persisted base
+ // monitor predates the chain data that triggered the refusal, as chain-driven
+ // full monitor writes are throttled. Either way, the update's steps are applied
+ // to the replayed monitor just as they were to the live one, so continue with
+ // the remaining updates rather than refusing to restore the monitor. Note the
+ // replay only matches the live application modulo chain-data-gated side
+ // effects, such as failing back outbound HTLCs after a funding spend.
log_error!(
self.logger,
"Monitor update failed. monitor: {} update: {} reason: {:?}",
monitor_key,
update_name.as_str(),
e
);
- io::Error::new(io::ErrorKind::Other, "Monitor update failed")
- })?;
+ }
}
Ok(Some((best_block, monitor)))
}
### pending_changelog/5640-refused-monitor-updates-persisted.txt
@@ -0,0 +1,11 @@
+## Bug Fixes
+ * When a `ChannelMonitor` refuses a `ChannelMonitorUpdate` because the funding
+ output was already spent on-chain, the update is now persisted like any
+ other update rather than only as part of a full monitor write. With
+ asynchronous persistence, the refused update was previously not tracked as
+ pending, so `MonitorEvent::Completed` for a later update could imply that
+ the refused update (which may carry a forwarded HTLC's preimage) was durable
+ before the write containing it landed (#5640). Restoring a monitor with the
+ built-in persisters previously failed entirely if replaying a stored update
+ was refused by the `ChannelMonitor`; the refusal is now logged and the
+ monitor is restored anyway.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.