Skip to content

Commit 1f53c04

Browse files
committed
Fix incorrect docs/disconnect handling in peer_handler
The way PeerHandler was written, it was supposed to remove from self.peers iff the API docs indicate that disconnect_event should NOT be called (and otherwise rely on disconnect_event to do so). Sadly, the implementation was way out of whack with reality - in the implementation, essentially anywhere where PeerHandler originated the disconnection, the peer was removed and no disconnect_event was expected. The docs, however, indicated that disconnect_event should nearly only be called, only not doing so when the initial handshake message never completed. We opt to change the docs, mostly, as well as clean up the ping/pong handling somewhat.
1 parent 3e726c4 commit 1f53c04

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,9 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
10361036

10371037
/// Indicates that the given socket descriptor's connection is now closed.
10381038
///
1039-
/// This must be called even if a PeerHandleError was given for a read_event or write_event,
1040-
/// but must NOT be called if a PeerHandleError was provided out of a new_\*\_connection event!
1039+
/// This must only be called if the socket has been lost and must NOT be called in any case
1040+
/// where other parts of this library (eg PeerHandleError, explicit disconnect_socket calls)
1041+
/// instruct you to disconnect the peer.
10411042
///
10421043
/// Panics if the descriptor was not previously registered in a successful new_*_connection event.
10431044
pub fn disconnect_event(&self, descriptor: &Descriptor) {
@@ -1073,10 +1074,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
10731074
let peers_needing_send = &mut peers.peers_needing_send;
10741075
let node_id_to_descriptor = &mut peers.node_id_to_descriptor;
10751076
let peers = &mut peers.peers;
1077+
let mut descriptors_needing_disconnect = Vec::new();
10761078

10771079
peers.retain(|descriptor, peer| {
10781080
if peer.awaiting_pong {
10791081
peers_needing_send.remove(descriptor);
1082+
descriptors_needing_disconnect.push(descriptor.clone());
10801083
match peer.their_node_id {
10811084
Some(node_id) => {
10821085
node_id_to_descriptor.remove(&node_id);
@@ -1104,6 +1107,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
11041107
peer.awaiting_pong = true;
11051108
true
11061109
});
1110+
1111+
for mut descriptor in descriptors_needing_disconnect.drain(..) {
1112+
descriptor.disconnect_socket();
1113+
}
11071114
}
11081115
}
11091116
}

0 commit comments

Comments
 (0)