Skip to content

Commit 98768f2

Browse files
committed
Add new DisconnectPeerWithWarning variant to ErrorAction
1 parent 9ea58cc commit 98768f2

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

lightning/src/ln/msgs.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,11 @@ pub enum ErrorAction {
11371137
/// An error message which we should make an effort to send before we disconnect.
11381138
msg: Option<ErrorMessage>
11391139
},
1140+
/// The peer did something incorrect. Tell them without closing any channels and disconnect them.
1141+
DisconnectPeerWithWarning {
1142+
/// A warning message which we should make an effort to send before we disconnect.
1143+
msg: WarningMessage,
1144+
},
11401145
/// The peer did something harmless that we weren't able to process, just log and ignore
11411146
// New code should *not* use this. New code must use IgnoreAndLog, below!
11421147
IgnoreError,

lightning/src/ln/peer_handler.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,21 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
12011201
Ok(x) => x,
12021202
Err(e) => {
12031203
match e.action {
1204-
msgs::ErrorAction::DisconnectPeer { msg: _ } => {
1205-
//TODO: Try to push msg
1204+
msgs::ErrorAction::DisconnectPeer { .. } => {
1205+
// We may have an `ErrorMessage` to send to the peer,
1206+
// but writing to the socket while reading can lead to
1207+
// re-entrant code and possibly unexpected behavior. The
1208+
// message send is optimistic anyway, and in this case
1209+
// we immediately disconnect the peer.
1210+
log_debug!(self.logger, "Error handling message{}; disconnecting peer with: {}", OptionalFromDebugger(&peer_node_id), e.err);
1211+
return Err(PeerHandleError { });
1212+
},
1213+
msgs::ErrorAction::DisconnectPeerWithWarning { .. } => {
1214+
// We have a `WarningMessage` to send to the peer, but
1215+
// writing to the socket while reading can lead to
1216+
// re-entrant code and possibly unexpected behavior. The
1217+
// message send is optimistic anyway, and in this case
1218+
// we immediately disconnect the peer.
12061219
log_debug!(self.logger, "Error handling message{}; disconnecting peer with: {}", OptionalFromDebugger(&peer_node_id), e.err);
12071220
return Err(PeerHandleError { });
12081221
},
@@ -1337,7 +1350,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
13371350
Ok(x) => x,
13381351
Err(e) => {
13391352
match e {
1340-
// Note that to avoid recursion we never call
1353+
// Note that to avoid re-entrancy we never call
13411354
// `do_attempt_write_data` from here, causing
13421355
// the messages enqueued here to not actually
13431356
// be sent before the peer is disconnected.
@@ -2046,13 +2059,29 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20462059
log_pubkey!(node_id), msg.contents.short_channel_id);
20472060
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
20482061
},
2049-
MessageSendEvent::HandleError { ref node_id, ref action } => {
2050-
match *action {
2051-
msgs::ErrorAction::DisconnectPeer { ref msg } => {
2062+
MessageSendEvent::HandleError { node_id, action } => {
2063+
match action {
2064+
msgs::ErrorAction::DisconnectPeer { msg } => {
2065+
if let Some(msg) = msg.as_ref() {
2066+
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
2067+
log_pubkey!(node_id), msg.data);
2068+
} else {
2069+
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {}",
2070+
log_pubkey!(node_id));
2071+
}
2072+
// We do not have the peers write lock, so we just store that we're
2073+
// about to disconenct the peer and do it after we finish
2074+
// processing most messages.
2075+
let msg = msg.map(|msg| wire::Message::<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>::Error(msg));
2076+
peers_to_disconnect.insert(node_id, msg);
2077+
},
2078+
msgs::ErrorAction::DisconnectPeerWithWarning { msg } => {
2079+
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
2080+
log_pubkey!(node_id), msg.data);
20522081
// We do not have the peers write lock, so we just store that we're
20532082
// about to disconenct the peer and do it after we finish
20542083
// processing most messages.
2055-
peers_to_disconnect.insert(*node_id, msg.clone());
2084+
peers_to_disconnect.insert(node_id, Some(wire::Message::Warning(msg)));
20562085
},
20572086
msgs::ErrorAction::IgnoreAndLog(level) => {
20582087
log_given_level!(self.logger, level, "Received a HandleError event to be ignored for node {}", log_pubkey!(node_id));
@@ -2065,13 +2094,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20652094
log_trace!(self.logger, "Handling SendErrorMessage HandleError event in peer_handler for node {} with message {}",
20662095
log_pubkey!(node_id),
20672096
msg.data);
2068-
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
2097+
self.enqueue_message(&mut *get_peer_for_forwarding!(&node_id), msg);
20692098
},
20702099
msgs::ErrorAction::SendWarningMessage { ref msg, ref log_level } => {
20712100
log_given_level!(self.logger, *log_level, "Handling SendWarningMessage HandleError event in peer_handler for node {} with message {}",
20722101
log_pubkey!(node_id),
20732102
msg.data);
2074-
self.enqueue_message(&mut *get_peer_for_forwarding!(node_id), msg);
2103+
self.enqueue_message(&mut *get_peer_for_forwarding!(&node_id), msg);
20752104
},
20762105
}
20772106
},
@@ -2121,9 +2150,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
21212150
if let Some(peer_mutex) = peers.remove(&descriptor) {
21222151
let mut peer = peer_mutex.lock().unwrap();
21232152
if let Some(msg) = msg {
2124-
log_trace!(self.logger, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
2125-
log_pubkey!(node_id),
2126-
msg.data);
21272153
self.enqueue_message(&mut *peer, &msg);
21282154
// This isn't guaranteed to work, but if there is enough free
21292155
// room in the send buffer, put the error message there...

0 commit comments

Comments
 (0)