Skip to content

Commit b101bfa

Browse files
Lock pending inbound and outbound payments to before channel_state
As the `channel_state` lock will be removed, this PR prepare for that by flipping the lock order for `pending_inbound_payments` and `pending_outbound_payments` locks to before the `channel_state` lock.
1 parent f76d011 commit b101bfa

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -674,25 +674,25 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
674674
// |
675675
// |__`forward_htlcs`
676676
// |
677-
// |__`channel_state`
677+
// |__`pending_inbound_payments`
678678
// | |
679-
// | |__`id_to_peer`
679+
// | |__`claimable_htlcs`
680680
// | |
681-
// | |__`per_peer_state`
681+
// | |__`pending_outbound_payments`
682682
// | |
683-
// | |__`outbound_scid_aliases`
684-
// | |
685-
// | |__`pending_inbound_payments`
683+
// | |__`channel_state`
684+
// | |
685+
// | |__`id_to_peer`
686686
// | |
687-
// | |__`claimable_htlcs`
687+
// | |__`pending_events`
688+
// | | |
689+
// | | |__`pending_background_events`
688690
// | |
689-
// | |__`pending_outbound_payments`
691+
// | |__`per_peer_state`
690692
// | |
691-
// | |__`best_block`
693+
// | |__`outbound_scid_aliases`
692694
// | |
693-
// | |__`pending_events`
694-
// | |
695-
// | |__`pending_background_events`
695+
// | |__`best_block`
696696
//
697697
pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
698698
where M::Target: chain::Watch<Signer>,
@@ -2455,7 +2455,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24552455
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
24562456

24572457
let err: Result<(), _> = loop {
2458-
let mut channel_lock = self.channel_state.lock().unwrap();
24592458

24602459
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
24612460
let payment_entry = pending_outbounds.entry(payment_id);
@@ -2467,6 +2466,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24672466
}
24682467
}
24692468

2469+
let mut channel_lock = self.channel_state.lock().unwrap();
2470+
24702471
let id = match channel_lock.short_to_chan_info.get(&path.first().unwrap().short_channel_id) {
24712472
None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}),
24722473
Some((_cp_id, chan_id)) => chan_id.clone(),
@@ -6657,14 +6658,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
66576658
}
66586659
}
66596660

6660-
let per_peer_state = self.per_peer_state.write().unwrap();
6661-
(per_peer_state.len() as u64).write(writer)?;
6662-
for (peer_pubkey, peer_state_mutex) in per_peer_state.iter() {
6663-
peer_pubkey.write(writer)?;
6664-
let peer_state = peer_state_mutex.lock().unwrap();
6665-
peer_state.latest_features.write(writer)?;
6661+
{
6662+
let per_peer_state = self.per_peer_state.write().unwrap();
6663+
(per_peer_state.len() as u64).write(writer)?;
6664+
for (peer_pubkey, peer_state_mutex) in per_peer_state.iter() {
6665+
peer_pubkey.write(writer)?;
6666+
let peer_state = peer_state_mutex.lock().unwrap();
6667+
peer_state.latest_features.write(writer)?;
6668+
}
66666669
}
66676670

6671+
66686672
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
66696673
let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
66706674
let events = self.pending_events.lock().unwrap();

0 commit comments

Comments
 (0)