Skip to content

Commit 2acebb9

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 b435384 commit 2acebb9

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
@@ -8178,8 +8178,6 @@ where
81788178
}
81798179

81808180
fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
8181-
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8182-
81838181
match &msg.data as &str {
81848182
"cannot co-op close channel w/ active htlcs"|
81858183
"link failed to shutdown" =>
@@ -8192,34 +8190,45 @@ where
81928190
// We're not going to bother handling this in a sensible way, instead simply
81938191
// repeating the Shutdown message on repeat until morale improves.
81948192
if !msg.channel_id.is_zero() {
8195-
let per_peer_state = self.per_peer_state.read().unwrap();
8196-
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8197-
if peer_state_mutex_opt.is_none() { return; }
8198-
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
8199-
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
8200-
if let Some(msg) = chan.get_outbound_shutdown() {
8201-
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
8202-
node_id: *counterparty_node_id,
8203-
msg,
8204-
});
8205-
}
8206-
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
8207-
node_id: *counterparty_node_id,
8208-
action: msgs::ErrorAction::SendWarningMessage {
8209-
msg: msgs::WarningMessage {
8210-
channel_id: msg.channel_id,
8211-
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
8212-
},
8213-
log_level: Level::Trace,
8193+
PersistenceNotifierGuard::optionally_notify(
8194+
self,
8195+
|| -> NotifyOption {
8196+
let per_peer_state = self.per_peer_state.read().unwrap();
8197+
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
8198+
if peer_state_mutex_opt.is_none() { return NotifyOption::SkipPersistNoEvents; }
8199+
let mut peer_state = peer_state_mutex_opt.unwrap().lock().unwrap();
8200+
if let Some(ChannelPhase::Funded(chan)) = peer_state.channel_by_id.get(&msg.channel_id) {
8201+
if let Some(msg) = chan.get_outbound_shutdown() {
8202+
peer_state.pending_msg_events.push(events::MessageSendEvent::SendShutdown {
8203+
node_id: *counterparty_node_id,
8204+
msg,
8205+
});
8206+
}
8207+
peer_state.pending_msg_events.push(events::MessageSendEvent::HandleError {
8208+
node_id: *counterparty_node_id,
8209+
action: msgs::ErrorAction::SendWarningMessage {
8210+
msg: msgs::WarningMessage {
8211+
channel_id: msg.channel_id,
8212+
data: "You appear to be exhibiting LND bug 6039, we'll keep sending you shutdown messages until you handle them correctly".to_owned()
8213+
},
8214+
log_level: Level::Trace,
8215+
}
8216+
});
8217+
// This can happen in a fairly tight loop, so we absolutely cannot trigger
8218+
// a `ChannelManager` write here.
8219+
return NotifyOption::SkipPersistHandleEvents;
82148220
}
8215-
});
8216-
}
8221+
NotifyOption::SkipPersistNoEvents
8222+
}
8223+
);
82178224
}
82188225
return;
82198226
}
82208227
_ => {}
82218228
}
82228229

8230+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8231+
82238232
if msg.channel_id.is_zero() {
82248233
let channel_ids: Vec<ChannelId> = {
82258234
let per_peer_state = self.per_peer_state.read().unwrap();

0 commit comments

Comments
 (0)