Skip to content

Commit

Permalink
fix(client): fix panic in Pool::connect
Browse files Browse the repository at this point in the history
Closes #780
  • Loading branch information
seanmonstar committed May 5, 2016
1 parent 1d936fe commit e51bafe
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/client/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,31 @@ impl<C: NetworkConnector<Stream=S>, S: NetworkStream + Send> NetworkConnector fo
type Stream = PooledStream<S>;
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<PooledStream<S>> {
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,
Expand Down

0 comments on commit e51bafe

Please sign in to comment.