Skip to content

Commit a9f3e45

Browse files
Break out pending_background_events into a sperate lock order branch
1 parent b101bfa commit a9f3e45

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
674674
// |
675675
// |__`forward_htlcs`
676676
// |
677+
// |__`pending_background_events`
678+
// |
677679
// |__`pending_inbound_payments`
678680
// | |
679681
// | |__`claimable_htlcs`
@@ -685,8 +687,6 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
685687
// | |__`id_to_peer`
686688
// | |
687689
// | |__`pending_events`
688-
// | | |
689-
// | | |__`pending_background_events`
690690
// | |
691691
// | |__`per_peer_state`
692692
// | |
@@ -5360,6 +5360,16 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
53605360
/// pushing the channel monitor update (if any) to the background events queue and removing the
53615361
/// Channel object.
53625362
fn handle_init_event_channel_failures(&self, mut failed_channels: Vec<ShutdownResult>) {
5363+
#[cfg(debug_assertions)]
5364+
{
5365+
// Ensure that the different lock branches are not held when calling this function.
5366+
// This ensures that future code doesn't introduce a lock_order requirement for
5367+
// `pending_background_events`.
5368+
assert!(self.channel_state.try_lock().is_ok());
5369+
assert!(self.per_peer_state.read().is_ok());
5370+
assert!(self.pending_inbound_payments.try_lock().is_ok());
5371+
assert!(self.pending_events.try_lock().is_ok());
5372+
}
53635373
for mut failure in failed_channels.drain(..) {
53645374
// Either a commitment transactions has been confirmed on-chain or
53655375
// Channel::block_disconnected detected that the funding transaction has been
@@ -6668,24 +6678,25 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
66686678
}
66696679
}
66706680

6671-
6672-
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
6673-
let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
6674-
let events = self.pending_events.lock().unwrap();
6675-
(events.len() as u64).write(writer)?;
6676-
for event in events.iter() {
6677-
event.write(writer)?;
6681+
{
6682+
let events = self.pending_events.lock().unwrap();
6683+
(events.len() as u64).write(writer)?;
6684+
for event in events.iter() {
6685+
event.write(writer)?;
6686+
}
66786687
}
66796688

6680-
let background_events = self.pending_background_events.lock().unwrap();
6681-
(background_events.len() as u64).write(writer)?;
6682-
for event in background_events.iter() {
6683-
match event {
6684-
BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)) => {
6685-
0u8.write(writer)?;
6686-
funding_txo.write(writer)?;
6687-
monitor_update.write(writer)?;
6688-
},
6689+
{
6690+
let background_events = self.pending_background_events.lock().unwrap();
6691+
(background_events.len() as u64).write(writer)?;
6692+
for event in background_events.iter() {
6693+
match event {
6694+
BackgroundEvent::ClosingMonitorUpdate((funding_txo, monitor_update)) => {
6695+
0u8.write(writer)?;
6696+
funding_txo.write(writer)?;
6697+
monitor_update.write(writer)?;
6698+
},
6699+
}
66896700
}
66906701
}
66916702

@@ -6695,12 +6706,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
66956706
(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
66966707
(self.highest_seen_timestamp.load(Ordering::Acquire) as u32).write(writer)?;
66976708

6709+
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
66986710
(pending_inbound_payments.len() as u64).write(writer)?;
66996711
for (hash, pending_payment) in pending_inbound_payments.iter() {
67006712
hash.write(writer)?;
67016713
pending_payment.write(writer)?;
67026714
}
67036715

6716+
let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
67046717
// For backwards compat, write the session privs and their total length.
67056718
let mut num_pending_outbounds_compat: u64 = 0;
67066719
for (_, outbound) in pending_outbound_payments.iter() {

0 commit comments

Comments
 (0)