Skip to content

Commit 03425e4

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 f5ee8c2 commit 03425e4

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
@@ -9242,8 +9242,6 @@ where
92429242
}
92439243

92449244
fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
9245-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9246-
92479245
match &msg.data as &str {
92489246
"cannot co-op close channel w/ active htlcs"|
92499247
"link failed to shutdown" =>
@@ -9256,34 +9254,45 @@ where
92569254
// We're not going to bother handling this in a sensible way, instead simply
92579255
// repeating the Shutdown message on repeat until morale improves.
92589256
if !msg.channel_id.is_zero() {
9259-
let per_peer_state = self.per_peer_state.read().unwrap();
9260-
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
9261-
if peer_state_mutex_opt.is_none() { return; }
9262-
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
9263-
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
9264-
if let Some(msg) = chan.get_outbound_shutdown() {
9265-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
9266-
node_id: *counterparty_node_id,
9267-
msg,
9268-
});
9269-
}
9270-
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
9271-
node_id: *counterparty_node_id,
9272-
action: msgs::ErrorAction::SendWarningMessage {
9273-
msg: msgs::WarningMessage {
9274-
channel_id: msg.channel_id,
9275-
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
9276-
},
9277-
log_level: Level::Trace,
9257+
PersistenceNotifierGuard::optionally_notify(
9258+
self,
9259+
|| -> NotifyOption {
9260+
let per_peer_state = self.per_peer_state.read().unwrap();
9261+
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
9262+
if peer_state_mutex_opt.is_none() { return NotifyOption::SkipPersistNoEvents; }
9263+
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
9264+
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
9265+
if let Some(msg) = chan.get_outbound_shutdown() {
9266+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
9267+
node_id: *counterparty_node_id,
9268+
msg,
9269+
});
9270+
}
9271+
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
9272+
node_id: *counterparty_node_id,
9273+
action: msgs::ErrorAction::SendWarningMessage {
9274+
msg: msgs::WarningMessage {
9275+
channel_id: msg.channel_id,
9276+
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
9277+
},
9278+
log_level: Level::Trace,
9279+
}
9280+
});
9281+
// This can happen in a fairly tight loop, so we absolutely cannot trigger
9282+
// a `ChannelManager` write here.
9283+
return NotifyOption::SkipPersistHandleEvents;
92789284
}
9279-
});
9280-
}
9285+
NotifyOption::SkipPersistNoEvents
9286+
}
9287+
);
92819288
}
92829289
return;
92839290
}
92849291
_ => {}
92859292
}
92869293

9294+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9295+
92879296
if msg.channel_id.is_zero() {
92889297
let channel_ids: Vec<ChannelId> = {
92899298
let per_peer_state = self.per_peer_state.read().unwrap();

0 commit comments

Comments
 (0)