Skip to content

Commit 2192da1

Browse files
f - Update lock order docs
1 parent feeaab9 commit 2192da1

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,20 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
669669
/// essentially you should default to using a SimpleRefChannelManager, and use a
670670
/// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
671671
/// you're using lightning-net-tokio.
672+
//
673+
// Lock order:
674+
// These `ChannelManager` locks should be aquired in the following lock order, if any of these
675+
// locks are held at the same time:
676+
// 1. `channel_state`
677+
// 2. `per_peer_state`
678+
// 3. `pending_inbound_payments`
679+
// 4. `pending_outbound_payments`
680+
// 5. `pending_events`
681+
// 6. `pending_background_events`
682+
//
683+
// These locks should be aquired after the `channel_state` lock, in case the `channel_state` lock
684+
// is taken at the same time:
685+
// * `id_to_peer`
672686
pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
673687
where M::Target: chain::Watch<Signer>,
674688
T::Target: BroadcasterInterface,
@@ -697,12 +711,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
697711
/// expose them to users via a PaymentReceived event. HTLCs which do not meet the requirements
698712
/// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
699713
/// after we generate a PaymentReceived upon receipt of all MPP parts or when they time out.
700-
///
701-
/// If any of these locks are held at the same time, they must be acquired prior and in the
702-
/// following order:
703-
/// * `channel_state`
704-
/// * `forward_htlcs`
705-
/// * `per_peer_state`
706714
pending_inbound_payments: Mutex<HashMap<PaymentHash, PendingInboundPayment>>,
707715

708716
/// The session_priv bytes and retry metadata of outbound payments which are pending resolution.
@@ -715,13 +723,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
715723
/// after reloading from disk while replaying blocks against ChannelMonitors.
716724
///
717725
/// See `PendingOutboundPayment` documentation for more info.
718-
///
719-
/// If any of these locks are held at the same time as, they must be acquired prior and in the
720-
/// following order:
721-
/// * `channel_state`
722-
/// * `forward_htlcs`
723-
/// * `per_peer_state`
724-
/// * `pending_inbound_payments`
725726
pending_outbound_payments: Mutex<HashMap<PaymentId, PendingOutboundPayment>>,
726727

727728
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
@@ -732,8 +733,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
732733
///
733734
/// Note that no consistency guarantees are made about the existence of a channel with the
734735
/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
735-
///
736-
/// Locked *after* `channel_state`.
737736
#[cfg(test)]
738737
pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
739738
#[cfg(not(test))]
@@ -762,8 +761,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
762761
/// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
763762
/// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
764763
/// required to access the channel with the `counterparty_node_id`.
765-
///
766-
/// Locked *after* `channel_state`.
767764
id_to_peer: Mutex<HashMap<[u8; 32], PublicKey>>,
768765

769766
our_network_key: SecretKey,
@@ -798,29 +795,10 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
798795
/// Because adding or removing an entry is rare, we usually take an outer read lock and then
799796
/// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
800797
/// new channel.
801-
///
802-
/// If any of these locks are held at the same time, they must be acquired prior and in the
803-
/// following order:
804-
/// * `channel_state`
805-
/// * `forward_htlcs`
806798
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
807799

808-
/// If any of these locks are held at the same time, they must be acquired prior and in the
809-
/// following order:
810-
/// * `channel_state`
811-
/// * `forward_htlcs`
812-
/// * `per_peer_state`
813-
/// * `pending_inbound_payments`
814-
/// * `pending_outbound_payments`
815800
pending_events: Mutex<Vec<events::Event>>,
816-
/// If any of these locks are held at the same time, they must be acquired prior and in the
817-
/// following order:
818-
/// * `channel_state`
819-
/// * `forward_htlcs`
820-
/// * `per_peer_state`
821-
/// * `pending_inbound_payments`
822-
/// * `pending_outbound_payments`
823-
/// * `pending_events`
801+
824802
pending_background_events: Mutex<Vec<BackgroundEvent>>,
825803
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
826804
/// Essentially just when we're serializing ourselves out.

0 commit comments

Comments
 (0)