From d2088ff48cd6cf1aec98238bd2310ffb4c1e9ad0 Mon Sep 17 00:00:00 2001 From: hashmap Date: Wed, 7 Nov 2018 01:42:36 +0100 Subject: [PATCH] Fix recently introduced peer deadlock (#1941) --- p2p/src/peer.rs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/p2p/src/peer.rs b/p2p/src/peer.rs index b1d17187f..78507defa 100644 --- a/p2p/src/peer.rs +++ b/p2p/src/peer.rs @@ -351,24 +351,12 @@ impl Peer { /// Stops the peer, closing its connection pub fn stop(&self) { - let _ = self - .connection - .as_ref() - .unwrap() - .lock() - .close_channel - .send(()); + stop_with_connection(&self.connection.as_ref().unwrap().lock()); } fn check_connection(&self) -> bool { - match self - .connection - .as_ref() - .unwrap() - .lock() - .error_channel - .try_recv() - { + let connection = self.connection.as_ref().unwrap().lock(); + match connection.error_channel.try_recv() { Ok(Error::Serialization(e)) => { let need_stop = { let mut state = self.state.write(); @@ -384,7 +372,7 @@ impl Peer { "Client {} corrupted, will disconnect ({:?}).", self.info.addr, e ); - self.stop(); + stop_with_connection(&connection); } false } @@ -400,7 +388,7 @@ impl Peer { }; if need_stop { debug!("Client {} connection lost: {:?}", self.info.addr, e); - self.stop(); + stop_with_connection(&connection); } false } @@ -412,6 +400,10 @@ impl Peer { } } +fn stop_with_connection(connection: &conn::Tracker) { + let _ = connection.close_channel.send(()); +} + /// Adapter implementation that forwards everything to an underlying adapter /// but keeps track of the block and transaction hashes that were received. #[derive(Clone)]