From e51bafe2e4f2a1efc36790232bef488c91131d0b Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 5 May 2016 10:45:16 -0700 Subject: [PATCH] fix(client): fix panic in Pool::connect Closes #780 --- src/client/pool.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/client/pool.rs b/src/client/pool.rs index fd86b0cd8c..f3198d5bf9 100644 --- a/src/client/pool.rs +++ b/src/client/pool.rs @@ -102,22 +102,31 @@ impl, S: NetworkStream + Send> NetworkConnector fo type Stream = PooledStream; fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result> { let key = key(host, port, scheme); - let mut should_remove = false; - let inner = match self.inner.lock().unwrap().conns.get_mut(&key) { - Some(ref mut vec) => { + + let inner = { + // keep the mutex locked only in this block + let mut locked = self.inner.lock().unwrap(); + let mut should_remove = false; + let inner = locked.conns.get_mut(&key).map(|vec| { trace!("Pool had connection, using"); should_remove = vec.len() == 1; vec.pop().unwrap() + }); + if should_remove { + locked.conns.remove(&key); } + inner + }; + + let inner = match inner { + Some(inner) => inner, None => PooledStreamInner { key: key.clone(), stream: try!(self.connector.connect(host, port, scheme)), previous_response_expected_no_content: false, } + }; - if should_remove { - self.inner.lock().unwrap().conns.remove(&key); - } Ok(PooledStream { inner: Some(inner), is_closed: false,