Skip to content

Commit edd732e

Browse files
committed
Fix incorrect docs around disconnect in peer_handler + rename fns
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 and rename a few functions to clarify what they actually do.
1 parent 3122d5c commit edd732e

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ pub struct MessageHandler<CM: Deref> where CM::Target: msgs::ChannelMessageHandl
4949
/// You probably want to just extend an int and put a file descriptor in a struct and implement
5050
/// send_data. Note that if you are using a higher-level net library that may close() itself, be
5151
/// careful to ensure you don't have races whereby you might register a new connection with an fd
52-
/// the same as a yet-to-be-disconnect_event()-ed.
52+
/// the same as a yet-to-be-socket_disconnected()-ed.
5353
pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
5454
/// Attempts to send some data from the given slice to the peer.
5555
///
5656
/// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
57-
/// Note that in the disconnected case, a disconnect_event must still fire and further write
57+
/// Note that in the disconnected case, a socket_disconnected must still fire and further write
5858
/// attempts may occur until that time.
5959
///
6060
/// If the returned size is smaller than data.len(), a write_available event must
@@ -67,17 +67,18 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
6767
/// *not* imply that further read events should be paused.
6868
fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize;
6969
/// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
70-
/// more calls to write_event, read_event or disconnect_event may be made with this descriptor.
71-
/// No disconnect_event should be generated as a result of this call, though obviously races
72-
/// may occur whereby disconnect_socket is called after a call to disconnect_event but prior to
73-
/// that event completing.
70+
/// more calls to write_buffer_space_avail, read_event or socket_disconnected may be made with
71+
/// this descriptor. No socket_disconnected call should be generated as a result of this call,
72+
/// though obviously races may occur whereby disconnect_socket is called after a call to
73+
/// socket_disconnected but prior to that event completing.
7474
fn disconnect_socket(&mut self);
7575
}
7676

7777
/// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
78-
/// generate no further read/write_events for the descriptor, only triggering a single
79-
/// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
80-
/// no such disconnect_event must be generated and the socket be silently disconencted).
78+
/// generate no further read_event/write_buffer_space_avail calls for the descriptor, only
79+
/// triggering a single socket_disconnected call (unless it was provided in response to a
80+
/// new_*_connection event, in which case no such socket_disconnected() must be generated and the
81+
/// socket be silently disconencted).
8182
pub struct PeerHandleError {
8283
/// Used to indicate that we probably can't make any future connections to this peer, implying
8384
/// we should go ahead and force-close any channels we have with it.
@@ -201,7 +202,7 @@ macro_rules! encode_msg {
201202
}
202203

203204
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
204-
/// PeerIds may repeat, but only after disconnect_event() has been called.
205+
/// PeerIds may repeat, but only after socket_disconnected() has been called.
205206
impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where CM::Target: msgs::ChannelMessageHandler {
206207
/// Constructs a new PeerManager with the given message handlers and node_id secret key
207208
/// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
@@ -254,13 +255,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
254255
}
255256

256257
/// Indicates a new outbound connection has been established to a node with the given node_id.
257-
/// Note that if an Err is returned here you MUST NOT call disconnect_event for the new
258+
/// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
258259
/// descriptor but must disconnect the connection immediately.
259260
///
260261
/// Returns a small number of bytes to send to the remote node (currently always 50).
261262
///
262-
/// Panics if descriptor is duplicative with some other descriptor which has not yet has a
263-
/// disconnect_event.
263+
/// Panics if descriptor is duplicative with some other descriptor which has not yet had a
264+
/// socket_disconnected().
264265
pub fn new_outbound_connection(&self, their_node_id: PublicKey, descriptor: Descriptor) -> Result<Vec<u8>, PeerHandleError> {
265266
let mut peer_encryptor = PeerChannelEncryptor::new_outbound(their_node_id.clone(), self.get_ephemeral_key());
266267
let res = peer_encryptor.get_act_one().to_vec();
@@ -294,11 +295,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
294295
///
295296
/// May refuse the connection by returning an Err, but will never write bytes to the remote end
296297
/// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
297-
/// call disconnect_event for the new descriptor but must disconnect the connection
298+
/// call socket_disconnected for the new descriptor but must disconnect the connection
298299
/// immediately.
299300
///
300-
/// Panics if descriptor is duplicative with some other descriptor which has not yet has a
301-
/// disconnect_event.
301+
/// Panics if descriptor is duplicative with some other descriptor which has not yet had
302+
/// socket_disconnected called.
302303
pub fn new_inbound_connection(&self, descriptor: Descriptor) -> Result<(), PeerHandleError> {
303304
let peer_encryptor = PeerChannelEncryptor::new_inbound(&self.our_node_secret);
304305
let pending_read_buffer = [0; 50].to_vec(); // Noise act one is 50 bytes
@@ -406,10 +407,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
406407
///
407408
/// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
408409
/// new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
409-
/// invariants around calling write_event in case a write did not fully complete must still
410-
/// hold - be ready to call write_event again if a write call generated here isn't sufficient!
411-
/// Panics if the descriptor was not previously registered in a new_\*_connection event.
412-
pub fn write_event(&self, descriptor: &mut Descriptor) -> Result<(), PeerHandleError> {
410+
/// invariants around calling write_buffer_space_avail in case a write did not fully complete
411+
/// must still hold - be ready to call write_buffer_space_avail again if a write call generated
412+
/// here isn't sufficient! Panics if the descriptor was not previously registered in a
413+
/// new_\*_connection event.
414+
pub fn write_buffer_space_avail(&self, descriptor: &mut Descriptor) -> Result<(), PeerHandleError> {
413415
let mut peers = self.peers.lock().unwrap();
414416
match peers.peers.get_mut(descriptor) {
415417
None => panic!("Descriptor for write_event is not already known to PeerManager"),
@@ -429,8 +431,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
429431
/// Thus, however, you almost certainly want to call process_events() after any read_event to
430432
/// generate send_data calls to handle responses.
431433
///
432-
/// If Ok(true) is returned, further read_events should not be triggered until a write_event on
433-
/// this file descriptor has resume_read set (preventing DoS issues in the send buffer).
434+
/// If Ok(true) is returned, further read_events should not be triggered until a send_data call
435+
/// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
434436
///
435437
/// Panics if the descriptor was not previously registered in a new_*_connection event.
436438
pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
@@ -1036,11 +1038,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
10361038

10371039
/// Indicates that the given socket descriptor's connection is now closed.
10381040
///
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!
1041+
/// This must only be called if the socket has been disconnected by the peer or your own
1042+
/// decision to disconnect it and must NOT be called in any case where other parts of this
1043+
/// library (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect
1044+
/// the peer.
10411045
///
10421046
/// Panics if the descriptor was not previously registered in a successful new_*_connection event.
1043-
pub fn disconnect_event(&self, descriptor: &Descriptor) {
1047+
pub fn socket_disconnected(&self, descriptor: &Descriptor) {
10441048
self.disconnect_event_internal(descriptor, false);
10451049
}
10461050

@@ -1073,10 +1077,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
10731077
let peers_needing_send = &mut peers.peers_needing_send;
10741078
let node_id_to_descriptor = &mut peers.node_id_to_descriptor;
10751079
let peers = &mut peers.peers;
1080+
let mut descriptors_needing_disconnect = Vec::new();
10761081

10771082
peers.retain(|descriptor, peer| {
10781083
if peer.awaiting_pong {
10791084
peers_needing_send.remove(descriptor);
1085+
descriptors_needing_disconnect.push(descriptor.clone());
10801086
match peer.their_node_id {
10811087
Some(node_id) => {
10821088
node_id_to_descriptor.remove(&node_id);
@@ -1104,6 +1110,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
11041110
peer.awaiting_pong = true;
11051111
true
11061112
});
1113+
1114+
for mut descriptor in descriptors_needing_disconnect.drain(..) {
1115+
descriptor.disconnect_socket();
1116+
}
11071117
}
11081118
}
11091119
}

0 commit comments

Comments
 (0)