]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Call `ChannelMessageHandler::message_received` without peer lock 2024-09-3010-followups
authorMatt Corallo <git@bluematt.me>
Thu, 12 Sep 2024 15:17:15 +0000 (15:17 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 30 Sep 2024 18:57:52 +0000 (18:57 +0000)
While `message_received` purports to be called on every message,
prior to the message, doing so on `Init` messages means we have to
call `message_received` while holding the per-peer mutex, which
can cause some lock contention.

Instead, here, we call `message_received` after processing `Init`
messages (which is probably more useful anyway - the peer isn't
really "connected" until we've processed the `Init` messages),
allowing us to call it unlocked.

lightning/src/ln/peer_handler.rs

index 3c0d724ad9423a2b18da59b734dd3e08d4bef287..6f0412ced4295e2ba812e098714fb537b430383a 100644 (file)
@@ -1618,14 +1618,15 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                let their_node_id = peer_lock.their_node_id.expect("We know the peer's public key by the time we receive messages").0;
                let logger = WithContext::from(&self.logger, Some(their_node_id), None, None);
 
+               let unprocessed_message = self.do_handle_message_holding_peer_lock(peer_lock, message, their_node_id, &logger)?;
+
                self.message_handler.chan_handler.message_received();
-               
-               let message = match self.do_handle_message_holding_peer_lock(peer_lock, message, their_node_id, &logger)? {
-                       Some(processed_message) => processed_message,
-                       None => return Ok(None),
-               };
 
-               self.do_handle_message_without_peer_lock(peer_mutex, message, their_node_id, &logger)
+               if let Some(message) = unprocessed_message {
+                       self.do_handle_message_without_peer_lock(peer_mutex, message, their_node_id, &logger)
+               } else {
+                       Ok(None)
+               }
        }
 
        // Conducts all message processing that requires us to hold the `peer_lock`.