Skip to content

Commit ebd1c56

Browse files
committed
fix(net): ignore NotConnected error in NetworkStream.close
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
1 parent 67340a5 commit ebd1c56

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/net.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A collection of traits abstracting over Listeners and Streams.
22
use std::any::{Any, TypeId};
33
use std::fmt;
4-
use std::io::{self, Read, Write};
4+
use std::io::{self, ErrorKind, Read, Write};
55
use std::net::{SocketAddr, ToSocketAddrs, TcpStream, TcpListener, Shutdown};
66
use std::mem;
77
use std::path::Path;
@@ -292,10 +292,20 @@ impl NetworkStream for HttpStream {
292292

293293
#[inline]
294294
fn close(&mut self, how: Shutdown) -> io::Result<()> {
295+
#[inline]
296+
fn shutdown(tcp: &mut TcpStream, how: Shutdown) -> io::Result<()> {
297+
match tcp.shutdown(how) {
298+
Ok(_) => Ok(()),
299+
Err(ref e) if e.kind() == ErrorKind::NotConnected => Ok(()),
300+
err => err
301+
}
302+
}
303+
295304
match *self {
296-
HttpStream::Http(ref mut inner) => inner.0.shutdown(how),
297-
HttpStream::Https(ref mut inner) => inner.get_mut().0.shutdown(how)
305+
HttpStream::Http(ref mut inner) => shutdown(&mut inner.0, how),
306+
HttpStream::Https(ref mut inner) => shutdown(&mut inner.get_mut().0, how)
298307
}
308+
299309
}
300310
}
301311

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

348358
#[cfg(test)]
349359
mod tests {
360+
use std::net::{Shutdown, TcpStream};
361+
350362
use mock::MockStream;
351-
use super::NetworkStream;
363+
use super::{HttpStream, NetworkStream, CloneTcpStream};
364+
365+
#[test]
366+
fn test_shutdown() {
367+
let tcp = CloneTcpStream(TcpStream::connect("0.0.0.0:1111").unwrap());
368+
let mut stream = HttpStream::Http(tcp);
369+
// see https://github.com/hyperium/hyper/issues/508
370+
stream.close(Shutdown::Write).unwrap();
371+
stream.close(Shutdown::Write).unwrap();
372+
}
352373

353374
#[test]
354375
fn test_downcast_box_stream() {

0 commit comments

Comments
 (0)