Skip to content

Commit

Permalink
Fix race between outbound messages and peer disconnection
Browse files Browse the repository at this point in the history
Previously, outbound messages held in `process_events` could race
with peer disconnection, allowing a message intended for a peer
before disconnection to be sent to the same peer after
disconnection.

The fix is simple - hold the peers read lock while we fetch
pending messages from peers (as we disconnect with the write lock).
  • Loading branch information
TheBlueMatt committed Oct 18, 2023
1 parent 1667ef5 commit 2c33d6d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1891,15 +1891,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
let flush_read_disabled = self.gossip_processing_backlog_lifted.swap(false, Ordering::Relaxed);

let mut peers_to_disconnect = HashMap::new();
let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
events_generated.append(&mut self.message_handler.route_handler.get_and_clear_pending_msg_events());

{
// TODO: There are some DoS attacks here where you can flood someone's outbound send
// buffer by doing things like announcing channels on another node. We should be willing to
// drop optional-ish messages when send buffers get full!

let peers_lock = self.peers.read().unwrap();

let mut events_generated = self.message_handler.chan_handler.get_and_clear_pending_msg_events();
events_generated.append(&mut self.message_handler.route_handler.get_and_clear_pending_msg_events());

let peers = &*peers_lock;
macro_rules! get_peer_for_forwarding {
($node_id: expr) => {
Expand Down

0 comments on commit 2c33d6d

Please sign in to comment.