Skip to content

Commit 186cd04

Browse files
Merge pull request #2158 from TheBlueMatt/2023-04-handle_err_more-check
Test for extra locks held in `handle_error` unconditionally
2 parents 1ceb41e + 9e6e20f commit 186cd04

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

lightning/src/ln/channelmanager.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -1354,15 +1354,15 @@ pub struct PhantomRouteHints {
13541354
}
13551355

13561356
macro_rules! handle_error {
1357-
($self: ident, $internal: expr, $counterparty_node_id: expr) => {
1357+
($self: ident, $internal: expr, $counterparty_node_id: expr) => { {
1358+
// In testing, ensure there are no deadlocks where the lock is already held upon
1359+
// entering the macro.
1360+
debug_assert_ne!($self.pending_events.held_by_thread(), LockHeldState::HeldByThread);
1361+
debug_assert_ne!($self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
1362+
13581363
match $internal {
13591364
Ok(msg) => Ok(msg),
13601365
Err(MsgHandleErrInternal { err, chan_id, shutdown_finish }) => {
1361-
// In testing, ensure there are no deadlocks where the lock is already held upon
1362-
// entering the macro.
1363-
debug_assert_ne!($self.pending_events.held_by_thread(), LockHeldState::HeldByThread);
1364-
debug_assert_ne!($self.per_peer_state.held_by_thread(), LockHeldState::HeldByThread);
1365-
13661366
let mut msg_events = Vec::with_capacity(2);
13671367

13681368
if let Some((shutdown_res, update_option)) = shutdown_finish {
@@ -1401,7 +1401,7 @@ macro_rules! handle_error {
14011401
Err(err)
14021402
},
14031403
}
1404-
}
1404+
} }
14051405
}
14061406

14071407
macro_rules! update_maps_on_chan_removal {
@@ -2786,29 +2786,34 @@ where
27862786

27872787
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
27882788
let peer_state = &mut *peer_state_lock;
2789-
let (chan, msg) = {
2790-
let (res, chan) = {
2791-
match peer_state.channel_by_id.remove(temporary_channel_id) {
2792-
Some(mut chan) => {
2793-
let funding_txo = find_funding_output(&chan, &funding_transaction)?;
2794-
2795-
(chan.get_outbound_funding_created(funding_transaction, funding_txo, &self.logger)
2796-
.map_err(|e| if let ChannelError::Close(msg) = e {
2797-
MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.get_user_id(), chan.force_shutdown(true), None)
2798-
} else { unreachable!(); })
2799-
, chan)
2789+
let (msg, chan) = match peer_state.channel_by_id.remove(temporary_channel_id) {
2790+
Some(mut chan) => {
2791+
let funding_txo = find_funding_output(&chan, &funding_transaction)?;
2792+
2793+
let funding_res = chan.get_outbound_funding_created(funding_transaction, funding_txo, &self.logger)
2794+
.map_err(|e| if let ChannelError::Close(msg) = e {
2795+
MsgHandleErrInternal::from_finish_shutdown(msg, chan.channel_id(), chan.get_user_id(), chan.force_shutdown(true), None)
2796+
} else { unreachable!(); });
2797+
match funding_res {
2798+
Ok(funding_msg) => (funding_msg, chan),
2799+
Err(_) => {
2800+
mem::drop(peer_state_lock);
2801+
mem::drop(per_peer_state);
2802+
2803+
let _ = handle_error!(self, funding_res, chan.get_counterparty_node_id());
2804+
return Err(APIError::ChannelUnavailable {
2805+
err: "Signer refused to sign the initial commitment transaction".to_owned()
2806+
});
28002807
},
2801-
None => { return Err(APIError::ChannelUnavailable { err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!(*temporary_channel_id), counterparty_node_id) }) },
28022808
}
2803-
};
2804-
match handle_error!(self, res, chan.get_counterparty_node_id()) {
2805-
Ok(funding_msg) => {
2806-
(chan, funding_msg)
2807-
},
2808-
Err(_) => { return Err(APIError::ChannelUnavailable {
2809-
err: "Signer refused to sign the initial commitment transaction".to_owned()
2810-
}) },
2811-
}
2809+
},
2810+
None => {
2811+
return Err(APIError::ChannelUnavailable {
2812+
err: format!(
2813+
"Channel with id {} not found for the passed counterparty node_id {}",
2814+
log_bytes!(*temporary_channel_id), counterparty_node_id),
2815+
})
2816+
},
28122817
};
28132818

28142819
peer_state.pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {

0 commit comments

Comments
 (0)