Skip to content

Commit 8a11db1

Browse files
committed
Split PeerManager::handle_message to avoid explicit mem::drop
Previously, `handle_message` was a single large method consisting of two logical parts: one modifying the peer state hence requiring us to hold the `peer_lock` `MutexGuard`, and, after calling `mem::drop(peer_lock)`, the remainder which does not only *not* require to hold the `MutexGuard`, but relies on it being dropped to avoid double-locking. However, the `mem::drop` was easily overlooked, making reasoning about lock orders etc. a headache. Here, we therefore have `handle_message` call two sub-methods reflecting the two logical parts, allowing us to avoid the explicit `mem::drop`, while at the same time making it less error-prone due to the two methods' signatures.
1 parent b8b1ef3 commit 8a11db1

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,15 +1588,36 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15881588
}
15891589

15901590
/// Process an incoming message and return a decision (ok, lightning error, peer handling error) regarding the next action with the peer
1591+
///
15911592
/// Returns the message back if it needs to be broadcasted to all other peers.
15921593
fn handle_message(
15931594
&self,
15941595
peer_mutex: &Mutex<Peer>,
1595-
mut peer_lock: MutexGuard<Peer>,
1596+
peer_lock: MutexGuard<Peer>,
15961597
message: wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>
15971598
) -> Result<Option<wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>>, MessageHandlingError> {
15981599
let their_node_id = peer_lock.their_node_id.clone().expect("We know the peer's public key by the time we receive messages").0;
15991600
let logger = WithContext::from(&self.logger, Some(their_node_id), None);
1601+
1602+
let message = match self.do_handle_message_holding_peer_lock(peer_lock, message, &their_node_id, &logger)? {
1603+
Some(processed_message) => processed_message,
1604+
None => return Ok(None),
1605+
};
1606+
1607+
self.do_handle_message_without_peer_lock(peer_mutex, message, &their_node_id, &logger)
1608+
}
1609+
1610+
// Conducts all message processing that requires us to hold the `peer_lock`.
1611+
//
1612+
// Returns the message back to allow for further subsequent processing using move semantics.
1613+
fn do_handle_message_holding_peer_lock<'a>(
1614+
&self,
1615+
mut peer_lock: MutexGuard<Peer>,
1616+
message: wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>,
1617+
their_node_id: &PublicKey,
1618+
logger: &WithContext<'a, L>
1619+
) -> Result<Option<wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>>, MessageHandlingError>
1620+
{
16001621
peer_lock.received_message_since_timer_tick = true;
16011622

16021623
// Need an Init as first message
@@ -1677,8 +1698,20 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
16771698
peer_lock.received_channel_announce_since_backlogged = true;
16781699
}
16791700

1680-
mem::drop(peer_lock);
1701+
Ok(Some(message))
1702+
}
16811703

1704+
// Conducts all message processing that doesn't require us to hold the `peer_lock`.
1705+
//
1706+
// Returns the message back to allow for further subsequent processing using move semantics.
1707+
fn do_handle_message_without_peer_lock<'a>(
1708+
&self,
1709+
peer_mutex: &Mutex<Peer>,
1710+
message: wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>,
1711+
their_node_id: &PublicKey,
1712+
logger: &WithContext<'a, L>
1713+
) -> Result<Option<wire::Message<<<CMH as core::ops::Deref>::Target as wire::CustomMessageReader>::CustomMessage>>, MessageHandlingError>
1714+
{
16821715
if is_gossip_msg(message.type_id()) {
16831716
log_gossip!(logger, "Received message {:?} from {}", message, log_pubkey!(their_node_id));
16841717
} else {

0 commit comments

Comments
 (0)