Skip to content

Commit ce36f2c

Browse files
committed
Acquire per_peer_state write lock earlier in ChannelManager::write
Previously, we would hold the `per_peer_state` `read` lock during the serialization of channel objects. While this might have reduced lock congestion minimally, we also calculated the number of serializable peers in this phase, only to use it later after dropping and reacquiring the `per_peer_state` `write` lock. This could potentially result in inconsistiencies if the `serializable_peer_count` would change after we dropped the `read` lock but before we acquired the `write` lock. To mitigate the issue we just acquire the `write` lock ealier and hold it for the remainder of `ChannelManager::write`.
1 parent ac9a2c8 commit ce36f2c

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

lightning/src/ln/channelmanager.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11035,9 +11035,10 @@ where
1103511035
best_block.block_hash.write(writer)?;
1103611036
}
1103711037

11038+
let per_peer_state = self.per_peer_state.write().unwrap();
11039+
1103811040
let mut serializable_peer_count: u64 = 0;
1103911041
{
11040-
let per_peer_state = self.per_peer_state.read().unwrap();
1104111042
let mut number_of_funded_channels = 0;
1104211043
for (_, peer_state_mutex) in per_peer_state.iter() {
1104311044
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
@@ -11084,8 +11085,6 @@ where
1108411085
decode_update_add_htlcs_opt = Some(decode_update_add_htlcs);
1108511086
}
1108611087

11087-
let per_peer_state = self.per_peer_state.write().unwrap();
11088-
1108911088
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
1109011089
let claimable_payments = self.claimable_payments.lock().unwrap();
1109111090
let pending_outbound_payments = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();

0 commit comments

Comments
 (0)