Skip to content

Commit

Permalink
fix(net): ignore NotConnected error in NetworkStream.close
Browse files Browse the repository at this point in the history
On OSX, calling shutdown a second time will return a NotConnected error.
This commit will just ignore it, since we can agree that if a stream is
"not connected", it is in fact "closed".

Closes #508
  • Loading branch information
seanmonstar committed May 7, 2015
1 parent 67340a5 commit ebd1c56
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A collection of traits abstracting over Listeners and Streams.
use std::any::{Any, TypeId};
use std::fmt;
use std::io::{self, Read, Write};
use std::io::{self, ErrorKind, Read, Write};
use std::net::{SocketAddr, ToSocketAddrs, TcpStream, TcpListener, Shutdown};
use std::mem;
use std::path::Path;
Expand Down Expand Up @@ -292,10 +292,20 @@ impl NetworkStream for HttpStream {

#[inline]
fn close(&mut self, how: Shutdown) -> io::Result<()> {
#[inline]
fn shutdown(tcp: &mut TcpStream, how: Shutdown) -> io::Result<()> {
match tcp.shutdown(how) {
Ok(_) => Ok(()),
Err(ref e) if e.kind() == ErrorKind::NotConnected => Ok(()),
err => err
}
}

match *self {
HttpStream::Http(ref mut inner) => inner.0.shutdown(how),
HttpStream::Https(ref mut inner) => inner.get_mut().0.shutdown(how)
HttpStream::Http(ref mut inner) => shutdown(&mut inner.0, how),
HttpStream::Https(ref mut inner) => shutdown(&mut inner.get_mut().0, how)
}

}
}

Expand Down Expand Up @@ -347,8 +357,19 @@ fn lift_ssl_error(ssl: SslError) -> io::Error {

#[cfg(test)]
mod tests {
use std::net::{Shutdown, TcpStream};

use mock::MockStream;
use super::NetworkStream;
use super::{HttpStream, NetworkStream, CloneTcpStream};

#[test]
fn test_shutdown() {
let tcp = CloneTcpStream(TcpStream::connect("0.0.0.0:1111").unwrap());
let mut stream = HttpStream::Http(tcp);
// see https://github.com/hyperium/hyper/issues/508
stream.close(Shutdown::Write).unwrap();
stream.close(Shutdown::Write).unwrap();
}

#[test]
fn test_downcast_box_stream() {
Expand Down

0 comments on commit ebd1c56

Please sign in to comment.