Skip to content

Commit 819146b

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 161ee62 commit 819146b

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
@@ -8745,8 +8745,6 @@ where
87458745
}
87468746

87478747
fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
8748-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8749-
87508748
match &msg.data as &str {
87518749
"cannot co-op close channel w/ active htlcs"|
87528750
"link failed to shutdown" =>
@@ -8759,34 +8757,45 @@ where
87598757
// We're not going to bother handling this in a sensible way, instead simply
87608758
// repeating the Shutdown message on repeat until morale improves.
87618759
if !msg.channel_id.is_zero() {
8762-
let per_peer_state = self.per_peer_state.read().unwrap();
8763-
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8764-
if peer_state_mutex_opt.is_none() { return; }
8765-
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
8766-
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
8767-
if let Some(msg) = chan.get_outbound_shutdown() {
8768-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
8769-
node_id: *counterparty_node_id,
8770-
msg,
8771-
});
8772-
}
8773-
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
8774-
node_id: *counterparty_node_id,
8775-
action: msgs::ErrorAction::SendWarningMessage {
8776-
msg: msgs::WarningMessage {
8777-
channel_id: msg.channel_id,
8778-
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
8779-
},
8780-
log_level: Level::Trace,
8760+
PersistenceNotifierGuard::optionally_notify(
8761+
self,
8762+
|| -> NotifyOption {
8763+
let per_peer_state = self.per_peer_state.read().unwrap();
8764+
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8765+
if peer_state_mutex_opt.is_none() { return NotifyOption::SkipPersistNoEvents; }
8766+
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
8767+
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
8768+
if let Some(msg) = chan.get_outbound_shutdown() {
8769+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
8770+
node_id: *counterparty_node_id,
8771+
msg,
8772+
});
8773+
}
8774+
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
8775+
node_id: *counterparty_node_id,
8776+
action: msgs::ErrorAction::SendWarningMessage {
8777+
msg: msgs::WarningMessage {
8778+
channel_id: msg.channel_id,
8779+
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
8780+
},
8781+
log_level: Level::Trace,
8782+
}
8783+
});
8784+
// This can happen in a fairly tight loop, so we absolutely cannot trigger
8785+
// a `ChannelManager` write here.
8786+
return NotifyOption::SkipPersistHandleEvents;
87818787
}
8782-
});
8783-
}
8788+
NotifyOption::SkipPersistNoEvents
8789+
}
8790+
);
87848791
}
87858792
return;
87868793
}
87878794
_ => {}
87888795
}
87898796

8797+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8798+
87908799
if msg.channel_id.is_zero() {
87918800
let channel_ids: Vec<ChannelId> = {
87928801
let per_peer_state = self.per_peer_state.read().unwrap();

0 commit comments

Comments
 (0)