Skip to content

Commit cd4b38d

Browse files
committed
Always call finish_force_close_channel on closure
This is a step towards more unified closing of channels, and provides a place where the per_peer_state lock is not held.
1 parent 1ac53ed commit cd4b38d

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2497,6 +2497,7 @@ where
24972497
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
24982498

24992499
let mut failed_htlcs: Vec<(HTLCSource, PaymentHash)>;
2500+
let mut shutdown_result = None;
25002501
loop {
25012502
let per_peer_state = self.per_peer_state.read().unwrap();
25022503

@@ -2541,6 +2542,7 @@ where
25412542
});
25422543
}
25432544
self.issue_channel_close_events(&chan.context, ClosureReason::HolderForceClosed);
2545+
shutdown_result = Some((None, Vec::new()));
25442546
}
25452547
}
25462548
break;
@@ -2562,6 +2564,10 @@ where
25622564
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
25632565
}
25642566

2567+
if let Some(shutdown_result) = shutdown_result {
2568+
self.finish_force_close_channel(shutdown_result);
2569+
}
2570+
25652571
Ok(())
25662572
}
25672573

@@ -3537,7 +3543,7 @@ where
35373543
///
35383544
/// See [`ChannelManager::send_preflight_probes`] for more information.
35393545
pub fn send_spontaneous_preflight_probes(
3540-
&self, node_id: PublicKey, amount_msat: u64, final_cltv_expiry_delta: u32,
3546+
&self, node_id: PublicKey, amount_msat: u64, final_cltv_expiry_delta: u32,
35413547
liquidity_limit_multiplier: Option<u64>,
35423548
) -> Result<Vec<(PaymentHash, PaymentId)>, ProbeSendFailure> {
35433549
let payment_params =
@@ -6082,6 +6088,7 @@ where
60826088
}
60836089

60846090
fn internal_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<(), MsgHandleErrInternal> {
6091+
let mut shutdown_result = None;
60856092
let per_peer_state = self.per_peer_state.read().unwrap();
60866093
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
60876094
.ok_or_else(|| {
@@ -6130,6 +6137,12 @@ where
61306137
});
61316138
}
61326139
self.issue_channel_close_events(&chan.context, ClosureReason::CooperativeClosure);
6140+
shutdown_result = Some((None, Vec::new()));
6141+
}
6142+
mem::drop(peer_state_mutex);
6143+
mem::drop(per_peer_state);
6144+
if let Some(shutdown_result) = shutdown_result {
6145+
self.finish_force_close_channel(shutdown_result);
61336146
}
61346147
Ok(())
61356148
}
@@ -6804,6 +6817,7 @@ where
68046817
fn maybe_generate_initial_closing_signed(&self) -> bool {
68056818
let mut handle_errors: Vec<(PublicKey, Result<(), _>)> = Vec::new();
68066819
let mut has_update = false;
6820+
let mut shutdown_result = None;
68076821
{
68086822
let per_peer_state = self.per_peer_state.read().unwrap();
68096823

@@ -6836,6 +6850,7 @@ where
68366850
log_info!(self.logger, "Broadcasting {}", log_tx!(tx));
68376851
self.tx_broadcaster.broadcast_transactions(&[&tx]);
68386852
update_maps_on_chan_removal!(self, &chan.context);
6853+
shutdown_result = Some((None, Vec::new()));
68396854
false
68406855
} else { true }
68416856
},
@@ -6857,6 +6872,10 @@ where
68576872
let _ = handle_error!(self, err, counterparty_node_id);
68586873
}
68596874

6875+
if let Some(shutdown_result) = shutdown_result {
6876+
self.finish_force_close_channel(shutdown_result);
6877+
}
6878+
68606879
has_update
68616880
}
68626881

@@ -7853,6 +7872,7 @@ where
78537872
// Clean up for removal.
78547873
update_maps_on_chan_removal!(self, &context);
78557874
self.issue_channel_close_events(&context, ClosureReason::DisconnectedPeer);
7875+
failed_channels.push((None, Vec::new()));
78567876
false
78577877
});
78587878
// Note that we don't bother generating any events for pre-accept channels -

0 commit comments

Comments
 (0)