Skip to content

Commit 367aa84

Browse files
f - Update lock order docs
1 parent 0c89eaf commit 367aa84

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
@@ -667,6 +667,20 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
667667
/// essentially you should default to using a SimpleRefChannelManager, and use a
668668
/// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
669669
/// you're using lightning-net-tokio.
670+
//
671+
// Lock order:
672+
// These `ChannelManager` locks should be aquired in the following lock order, if any of these
673+
// locks are held at the same time:
674+
// 1. `channel_state`
675+
// 2. `per_peer_state`
676+
// 3. `pending_inbound_payments`
677+
// 4. `pending_outbound_payments`
678+
// 5. `pending_events`
679+
// 6. `pending_background_events`
680+
//
681+
// These locks should be aquired after the `channel_state` lock, in case the `channel_state` lock
682+
// is taken at the same time:
683+
// * `id_to_peer`
670684
pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
671685
where M::Target: chain::Watch<Signer>,
672686
T::Target: BroadcasterInterface,
@@ -695,12 +709,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
695709
/// expose them to users via a PaymentReceived event. HTLCs which do not meet the requirements
696710
/// here are failed when we process them as pending-forwardable-HTLCs, and entries are removed
697711
/// after we generate a PaymentReceived upon receipt of all MPP parts or when they time out.
698-
///
699-
/// If any of these locks are held at the same time, they must be acquired prior and in the
700-
/// following order:
701-
/// * `channel_state`
702-
/// * `forward_htlcs`
703-
/// * `per_peer_state`
704712
pending_inbound_payments: Mutex<HashMap<PaymentHash, PendingInboundPayment>>,
705713

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

725726
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
@@ -730,8 +731,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
730731
///
731732
/// Note that no consistency guarantees are made about the existence of a channel with the
732733
/// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
733-
///
734-
/// Locked *after* `channel_state`.
735734
#[cfg(test)]
736735
pub(super) forward_htlcs: Mutex<HashMap<u64, Vec<HTLCForwardInfo>>>,
737736
#[cfg(not(test))]
@@ -760,8 +759,6 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
760759
/// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
761760
/// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
762761
/// required to access the channel with the `counterparty_node_id`.
763-
///
764-
/// Locked *after* `channel_state`.
765762
id_to_peer: Mutex<HashMap<[u8; 32], PublicKey>>,
766763

767764
our_network_key: SecretKey,
@@ -792,29 +789,10 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
792789
/// Because adding or removing an entry is rare, we usually take an outer read lock and then
793790
/// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
794791
/// new channel.
795-
///
796-
/// If any of these locks are held at the same time, they must be acquired prior and in the
797-
/// following order:
798-
/// * `channel_state`
799-
/// * `forward_htlcs`
800792
per_peer_state: RwLock<HashMap<PublicKey, Mutex<PeerState>>>,
801793

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`
806-
/// * `per_peer_state`
807-
/// * `pending_inbound_payments`
808-
/// * `pending_outbound_payments`
809794
pending_events: Mutex<Vec<events::Event>>,
810-
/// If any of these locks are held at the same time, they must be acquired prior and in the
811-
/// following order:
812-
/// * `channel_state`
813-
/// * `forward_htlcs`
814-
/// * `per_peer_state`
815-
/// * `pending_inbound_payments`
816-
/// * `pending_outbound_payments`
817-
/// * `pending_events`
795+
818796
pending_background_events: Mutex<Vec<BackgroundEvent>>,
819797
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
820798
/// Essentially just when we're serializing ourselves out.

0 commit comments

Comments
 (0)