Skip to content

Commit 9737a01

Browse files
committed
Call peer_disconnected after a handler refuses a connection
If one message handler refuses a connection by returning an `Err` from `peer_connected`, other handlers which already got the `peer_connected` will not see the corresponding `peer_disconnected`, leaving them in a potentially-inconsistent state. Here we ensure we call the `peer_disconnected` handler for all handlers which received a `peer_connected` event (except the one which refused the connection).
1 parent b1fc7d8 commit 9737a01

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lightning/src/ln/msgs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,8 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
15781578
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
15791579
/// with us. Implementors should be somewhat conservative about doing so, however, as other
15801580
/// message handlers may still wish to communicate with this peer.
1581+
///
1582+
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
15811583
fn peer_connected(&self, their_node_id: PublicKey, msg: &Init, inbound: bool) -> Result<(), ()>;
15821584
/// Handle an incoming `channel_reestablish` message from the given peer.
15831585
fn handle_channel_reestablish(&self, their_node_id: PublicKey, msg: &ChannelReestablish);
@@ -1707,6 +1709,8 @@ pub trait OnionMessageHandler {
17071709
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
17081710
/// with us. Implementors should be somewhat conservative about doing so, however, as other
17091711
/// message handlers may still wish to communicate with this peer.
1712+
///
1713+
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
17101714
fn peer_connected(&self, their_node_id: PublicKey, init: &Init, inbound: bool) -> Result<(), ()>;
17111715

17121716
/// Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to

lightning/src/ln/peer_handler.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ pub trait CustomMessageHandler: wire::CustomMessageReader {
8888
/// May return an `Err(())` if the features the peer supports are not sufficient to communicate
8989
/// with us. Implementors should be somewhat conservative about doing so, however, as other
9090
/// message handlers may still wish to communicate with this peer.
91+
///
92+
/// [`Self::peer_disconnected`] will not be called if `Err(())` is returned.
9193
fn peer_connected(&self, their_node_id: PublicKey, msg: &Init, inbound: bool) -> Result<(), ()>;
9294

9395
/// Gets the node feature flags which this handler itself supports. All available handlers are
@@ -1718,10 +1720,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
17181720
}
17191721
if let Err(()) = self.message_handler.onion_message_handler.peer_connected(their_node_id, &msg, peer_lock.inbound_connection) {
17201722
log_debug!(logger, "Onion Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
1723+
self.message_handler.chan_handler.peer_disconnected(their_node_id);
17211724
return Err(PeerHandleError { }.into());
17221725
}
17231726
if let Err(()) = self.message_handler.custom_message_handler.peer_connected(their_node_id, &msg, peer_lock.inbound_connection) {
17241727
log_debug!(logger, "Custom Message Handler decided we couldn't communicate with peer {}", log_pubkey!(their_node_id));
1728+
self.message_handler.chan_handler.peer_disconnected(their_node_id);
1729+
self.message_handler.onion_message_handler.peer_disconnected(their_node_id);
17251730
return Err(PeerHandleError { }.into());
17261731
}
17271732

0 commit comments

Comments
 (0)