@@ -8863,29 +8863,45 @@ where
8863
8863
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
8864
8864
let peer_state = &mut *peer_state_lock;
8865
8865
let pending_msg_events = &mut peer_state.pending_msg_events;
8866
- peer_state.channel_by_id.retain(|_, phase| {
8866
+
8867
+ let mut remove_keys = Vec::new();
8868
+
8869
+ for (chan_id, phase) in peer_state.channel_by_id.iter_mut() {
8867
8870
let context = match phase {
8868
8871
ChannelPhase::Funded(chan) => {
8869
8872
if chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger).is_ok() {
8870
8873
// We only retain funded channels that are not shutdown.
8871
- return true ;
8874
+ continue ;
8872
8875
}
8873
8876
&mut chan.context
8874
8877
},
8875
- // Unfunded channels will always be removed.
8876
8878
ChannelPhase::UnfundedOutboundV1(chan) => {
8879
+ if peer_state.unnotified_outbound_channel.contains_key(chan_id) {
8880
+ // We also retain channel that were created but not broadcasted
8881
+ // due to peer disconnecting
8882
+ continue;
8883
+ }
8877
8884
&mut chan.context
8878
8885
},
8879
8886
ChannelPhase::UnfundedInboundV1(chan) => {
8880
8887
&mut chan.context
8881
8888
},
8882
8889
};
8890
+
8883
8891
// Clean up for removal.
8884
8892
update_maps_on_chan_removal!(self, &context);
8885
8893
self.issue_channel_close_events(&context, ClosureReason::DisconnectedPeer);
8886
8894
failed_channels.push(context.force_shutdown(false));
8887
- false
8888
- });
8895
+
8896
+ // Mark the channels for removal that didn't satisfy the "continue" conditions.
8897
+ remove_keys.push(chan_id.clone()); // Collect keys to remove
8898
+ }
8899
+
8900
+ // Remove channels based on the keys collected.
8901
+ for key in remove_keys {
8902
+ peer_state.channel_by_id.remove(&key);
8903
+ }
8904
+
8889
8905
// Note that we don't bother generating any events for pre-accept channels -
8890
8906
// they're not considered "channels" yet from the PoV of our events interface.
8891
8907
peer_state.inbound_channel_request_by_id.clear();
@@ -9034,23 +9050,28 @@ where
9034
9050
});
9035
9051
}
9036
9052
9037
- peer_state.unnotified_outbound_channel.clear();
9038
-
9039
- peer_state.channel_by_id.iter_mut().filter_map(|(_, phase)|
9040
- if let ChannelPhase::Funded(chan) = phase { Some(chan) } else {
9041
- // Since unfunded channel maps are cleared upon disconnecting a peer, and they're not persisted
9042
- // (so won't be recovered after a crash), they shouldn't exist here and we would never need to
9043
- // worry about closing and removing them.
9044
-
9053
+ for (chan_id, phase) in peer_state.channel_by_id.iter_mut() {
9054
+ if let ChannelPhase::Funded(chan) = phase {
9055
+ pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
9056
+ node_id: chan.context.get_counterparty_node_id(),
9057
+ msg: chan.get_channel_reestablish(&self.logger),
9058
+ });
9059
+ }
9060
+ else if peer_state.unnotified_outbound_channel.contains_key(chan_id) {
9061
+ // That means the channel is notified about just now.
9062
+ continue;
9063
+ }
9064
+ else {
9065
+ // Since unfunded channel maps are cleared upon disconnecting a peer and they're not persisted
9066
+ // (so won't be recovered after a crash), they shouldn't exist here, and we would never need to
9067
+ // worry about closing and removing them. However, it's important to note that if we were able to
9068
+ // create a channel but failed to send the `SendOpenChannel` message, the channel information would
9069
+ // be persisted.
9045
9070
debug_assert!(false);
9046
- None
9047
9071
}
9048
- ).for_each(|chan| {
9049
- pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
9050
- node_id: chan.context.get_counterparty_node_id(),
9051
- msg: chan.get_channel_reestablish(&self.logger),
9052
- });
9053
- });
9072
+ }
9073
+
9074
+ peer_state.unnotified_outbound_channel.clear();
9054
9075
}
9055
9076
9056
9077
return NotifyOption::SkipPersistHandleEvents;
0 commit comments