From 9e7d4324f57d6222fce201acdfb86d25dd8d639e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 22 Aug 2022 04:14:24 +0000 Subject: [PATCH] Don't hold the counter lock while verifying gossip/waiting on DB This resolves a deadlock if we block on the DB where we have one thread blocked waiting on DB in a blocking thread, and the tokio reactor blocked waiting on the counter lock which the blocking thread holds. --- src/downloader.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index 6bfaa14..997e997 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -57,16 +57,19 @@ impl RoutingMessageHandler for GossipRouter { } fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result { - let mut counter = self.counter.write().unwrap(); - - let output_value = self.native_router.handle_channel_announcement(msg).map_err(|error| { - if error.err.contains("didn't match on-chain script") { - counter.channel_announcements_with_mismatched_scripts += 1; - } - error - })?; + let native_result = self.native_router.handle_channel_announcement(msg); + let output_value; + { + let mut counter = self.counter.write().unwrap(); + output_value = native_result.map_err(|error| { + if error.err.contains("didn't match on-chain script") { + counter.channel_announcements_with_mismatched_scripts += 1; + } + error + })?; + counter.channel_announcements += 1; + } - counter.channel_announcements += 1; let gossip_message = GossipMessage::ChannelAnnouncement(msg.clone()); if let Err(err) = self.sender.try_send(gossip_message) { let gossip_message = match err { TrySendError::Full(msg)|TrySendError::Closed(msg) => msg }; @@ -81,8 +84,7 @@ impl RoutingMessageHandler for GossipRouter { fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result { let output_value = self.native_router.handle_channel_update(msg)?; - let mut counter = self.counter.write().unwrap(); - counter.channel_updates += 1; + self.counter.write().unwrap().channel_updates += 1; let gossip_message = GossipMessage::ChannelUpdate(msg.clone()); if let Err(err) = self.sender.try_send(gossip_message) { -- 2.39.5