@@ -3509,6 +3509,7 @@ macro_rules! emit_initial_channel_ready_event {
35093509macro_rules! handle_monitor_update_completion {
35103510 ($self: ident, $peer_state_lock: expr, $peer_state: expr, $per_peer_state_lock: expr, $chan: expr) => { {
35113511 let channel_id = $chan.context.channel_id();
3512+ let outbound_scid_alias = $chan.context().outbound_scid_alias();
35123513 let counterparty_node_id = $chan.context.get_counterparty_node_id();
35133514 #[cfg(debug_assertions)]
35143515 {
@@ -3521,7 +3522,7 @@ macro_rules! handle_monitor_update_completion {
35213522 let mut updates = $chan.monitor_updating_restored(&&logger,
35223523 &$self.node_signer, $self.chain_hash, &*$self.config.read().unwrap(),
35233524 $self.best_block.read().unwrap().height,
3524- |htlc_id| $self.path_for_release_held_htlc(htlc_id, &channel_id, &counterparty_node_id));
3525+ |htlc_id| $self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &channel_id, &counterparty_node_id));
35253526 let channel_update = if updates.channel_ready.is_some()
35263527 && $chan.context.is_usable()
35273528 && $peer_state.is_connected
@@ -5623,11 +5624,17 @@ where
56235624 /// [`HeldHtlcAvailable`] onion message, so the recipient's [`ReleaseHeldHtlc`] response will be
56245625 /// received to our node.
56255626 fn path_for_release_held_htlc(
5626- &self, htlc_id: u64, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
5627+ &self, htlc_id: u64, prev_outbound_scid_alias: u64, channel_id: &ChannelId,
5628+ counterparty_node_id: &PublicKey,
56275629 ) -> BlindedMessagePath {
56285630 let intercept_id =
56295631 InterceptId::from_htlc_id_and_chan_id(htlc_id, channel_id, counterparty_node_id);
5630- self.flow.path_for_release_held_htlc(intercept_id, &*self.entropy_source)
5632+ self.flow.path_for_release_held_htlc(
5633+ intercept_id,
5634+ prev_outbound_scid_alias,
5635+ htlc_id,
5636+ &*self.entropy_source,
5637+ )
56315638 }
56325639
56335640 /// Signals that no further attempts for the given payment should occur. Useful if you have a
@@ -11302,14 +11309,15 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1130211309 // disconnect, so Channel's reestablish will never hand us any holding cell
1130311310 // freed HTLCs to fail backwards. If in the future we no longer drop pending
1130411311 // add-HTLCs on disconnect, we may be handed HTLCs to fail backwards here.
11312+ let outbound_scid_alias = chan.context.outbound_scid_alias();
1130511313 let res = chan.channel_reestablish(
1130611314 msg,
1130711315 &&logger,
1130811316 &self.node_signer,
1130911317 self.chain_hash,
1131011318 &self.config.read().unwrap(),
1131111319 &*self.best_block.read().unwrap(),
11312- |htlc_id| self.path_for_release_held_htlc(htlc_id, &msg.channel_id, counterparty_node_id)
11320+ |htlc_id| self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &msg.channel_id, counterparty_node_id)
1131311321 );
1131411322 let responses = try_channel_entry!(self, peer_state, res, chan_entry);
1131511323 let mut channel_update = None;
@@ -11786,11 +11794,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1178611794 // Returns whether we should remove this channel as it's just been closed.
1178711795 let unblock_chan = |chan: &mut Channel<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| -> Option<ShutdownResult> {
1178811796 let channel_id = chan.context().channel_id();
11797+ let outbound_scid_alias = chan.context().outbound_scid_alias();
1178911798 let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
1179011799 let node_id = chan.context().get_counterparty_node_id();
1179111800 if let Some(msgs) = chan.signer_maybe_unblocked(
1179211801 self.chain_hash, &&logger,
11793- |htlc_id| self.path_for_release_held_htlc(htlc_id, &channel_id, &node_id)
11802+ |htlc_id| self.path_for_release_held_htlc(htlc_id, outbound_scid_alias, &channel_id, &node_id)
1179411803 ) {
1179511804 if chan.context().is_connected() {
1179611805 if let Some(msg) = msgs.open_channel {
@@ -15029,7 +15038,34 @@ where
1502915038 );
1503015039 }
1503115040 },
15032- AsyncPaymentsContext::ReleaseHeldHtlc { intercept_id } => {
15041+ AsyncPaymentsContext::ReleaseHeldHtlc {
15042+ intercept_id,
15043+ prev_outbound_scid_alias,
15044+ htlc_id,
15045+ } => {
15046+ // It's possible the release_held_htlc message raced ahead of us transitioning the pending
15047+ // update_add to `Self::pending_intercept_htlcs`. If that's the case, update the pending
15048+ // update_add to indicate that the HTLC should be released immediately.
15049+ //
15050+ // Check for the HTLC here before checking `pending_intercept_htlcs` to avoid a different
15051+ // race where the HTLC gets transitioned to `pending_intercept_htlcs` after we drop that
15052+ // map's lock but before acquiring the `decode_update_add_htlcs` lock.
15053+ let mut decode_update_add_htlcs = self.decode_update_add_htlcs.lock().unwrap();
15054+ if let Some(htlcs) = decode_update_add_htlcs.get_mut(&prev_outbound_scid_alias) {
15055+ for update_add in htlcs.iter_mut() {
15056+ if update_add.htlc_id == htlc_id {
15057+ log_trace!(
15058+ self.logger,
15059+ "Marking held htlc with intercept_id {} as ready to release",
15060+ intercept_id
15061+ );
15062+ update_add.hold_htlc.take();
15063+ return;
15064+ }
15065+ }
15066+ }
15067+ core::mem::drop(decode_update_add_htlcs);
15068+
1503315069 let mut htlc = {
1503415070 let mut pending_intercept_htlcs =
1503515071 self.pending_intercepted_htlcs.lock().unwrap();
0 commit comments