Skip to content

Commit bae81ba

Browse files
committed
Swap per_peer_state lock order
1 parent 7e23afe commit bae81ba

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,13 +577,13 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> = C
577577
// | |
578578
// | |__`pending_intercepted_htlcs`
579579
// |
580-
// |__`pending_inbound_payments`
580+
// |__`per_peer_state`
581581
// | |
582-
// | |__`claimable_payments`
583-
// | |
584-
// | |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
582+
// | |__`pending_inbound_payments`
583+
// | |
584+
// | |__`claimable_payments`
585585
// | |
586-
// | |__`per_peer_state`
586+
// | |__`pending_outbound_payments` // This field's struct contains a map of pending outbounds
587587
// | |
588588
// | |__`peer_state`
589589
// | |
@@ -3570,9 +3570,12 @@ where
35703570
// Ensure that no peer state channel storage lock is not held when calling this
35713571
// function.
35723572
// This ensures that future code doesn't introduce a lock_order requirement for
3573-
// `forward_htlcs` to be locked after the `per_peer_state` locks, which calling this
3574-
// function with the `per_peer_state` aquired would.
3575-
assert!(self.per_peer_state.try_write().is_ok());
3573+
// `forward_htlcs` to be locked after the `per_peer_state` peer locks, which calling
3574+
// this function with any `per_peer_state` peer lock aquired would.
3575+
let per_peer_state = self.per_peer_state.read().unwrap();
3576+
for (_, peer) in per_peer_state.iter() {
3577+
assert!(peer.try_lock().is_ok());
3578+
}
35763579
}
35773580

35783581
//TODO: There is a timing attack here where if a node fails an HTLC back to us they can
@@ -6739,6 +6742,8 @@ where
67396742
}
67406743
}
67416744

6745+
let per_peer_state = self.per_peer_state.write().unwrap();
6746+
67426747
let pending_inbound_payments = self.pending_inbound_payments.lock().unwrap();
67436748
let claimable_payments = self.claimable_payments.lock().unwrap();
67446749
let pending_outbound_payments = self.pending_outbound_payments.pending_outbound_payments.lock().unwrap();
@@ -6754,7 +6759,6 @@ where
67546759
htlc_purposes.push(purpose);
67556760
}
67566761

6757-
let per_peer_state = self.per_peer_state.write().unwrap();
67586762
(per_peer_state.len() as u64).write(writer)?;
67596763
for (peer_pubkey, peer_state_mutex) in per_peer_state.iter() {
67606764
peer_pubkey.write(writer)?;

0 commit comments

Comments
 (0)