Skip to content

Commit 673a409

Browse files
committed
Avoid writing ChannelManager when hitting lnd bug 6039
When we hit lnd bug 6039, we end up sending error messages to peers in a loop. This should be fine, but because we used the generic `PersistenceNotifierGuard::notify_on_drop` lock above the specific handling, we end up writing `ChannelManager` every time we manage a round-trip to our peer. This can add up quite quickly, and isn't actually changing, so we really need to avoid writing the `ChannelManager` in this case.
1 parent e2108f2 commit 673a409

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9026,8 +9026,6 @@ where
90269026
}
90279027

90289028
fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
9029-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9030-
90319029
match &msg.data as &str {
90329030
"cannot co-op close channel w/ active htlcs"|
90339031
"link failed to shutdown" =>
@@ -9040,34 +9038,45 @@ where
90409038
// We're not going to bother handling this in a sensible way, instead simply
90419039
// repeating the Shutdown message on repeat until morale improves.
90429040
if !msg.channel_id.is_zero() {
9043-
let per_peer_state = self.per_peer_state.read().unwrap();
9044-
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
9045-
if peer_state_mutex_opt.is_none() { return; }
9046-
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
9047-
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
9048-
if let Some(msg) = chan.get_outbound_shutdown() {
9049-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
9050-
node_id: *counterparty_node_id,
9051-
msg,
9052-
});
9053-
}
9054-
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
9055-
node_id: *counterparty_node_id,
9056-
action: msgs::ErrorAction::SendWarningMessage {
9057-
msg: msgs::WarningMessage {
9058-
channel_id: msg.channel_id,
9059-
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
9060-
},
9061-
log_level: Level::Trace,
9041+
PersistenceNotifierGuard::optionally_notify(
9042+
self,
9043+
|| -> NotifyOption {
9044+
let per_peer_state = self.per_peer_state.read().unwrap();
9045+
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
9046+
if peer_state_mutex_opt.is_none() { return NotifyOption::SkipPersistNoEvents; }
9047+
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
9048+
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
9049+
if let Some(msg) = chan.get_outbound_shutdown() {
9050+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
9051+
node_id: *counterparty_node_id,
9052+
msg,
9053+
});
9054+
}
9055+
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
9056+
node_id: *counterparty_node_id,
9057+
action: msgs::ErrorAction::SendWarningMessage {
9058+
msg: msgs::WarningMessage {
9059+
channel_id: msg.channel_id,
9060+
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
9061+
},
9062+
log_level: Level::Trace,
9063+
}
9064+
});
9065+
// This can happen in a fairly tight loop, so we absolutely cannot trigger
9066+
// a `ChannelManager` write here.
9067+
return NotifyOption::SkipPersistHandleEvents;
90629068
}
9063-
});
9064-
}
9069+
NotifyOption::SkipPersistNoEvents
9070+
}
9071+
);
90659072
}
90669073
return;
90679074
}
90689075
_ => {}
90699076
}
90709077

9078+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9079+
90719080
if msg.channel_id.is_zero() {
90729081
let channel_ids: Vec<ChannelId> = {
90739082
let per_peer_state = self.per_peer_state.read().unwrap();

0 commit comments

Comments
 (0)