fix(client): close connections when Response Future or Body is dropped
Closes #1397
This commit is contained in:
@@ -704,6 +704,97 @@ mod dispatch_impl {
|
||||
assert_eq!(closes.load(Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_response_future_closes_in_progress_connection() {
|
||||
let _ = pretty_env_logger::init();
|
||||
|
||||
let server = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let addr = server.local_addr().unwrap();
|
||||
let mut core = Core::new().unwrap();
|
||||
let handle = core.handle();
|
||||
let closes = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
let (_client_drop_tx, client_drop_rx) = oneshot::channel::<()>();
|
||||
|
||||
thread::spawn(move || {
|
||||
let mut sock = server.accept().unwrap().0;
|
||||
sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
|
||||
sock.set_write_timeout(Some(Duration::from_secs(5))).unwrap();
|
||||
let mut buf = [0; 4096];
|
||||
sock.read(&mut buf).expect("read 1");
|
||||
// we never write a response head
|
||||
// simulates a slow server operation
|
||||
let _ = tx1.send(());
|
||||
|
||||
// prevent this thread from closing until end of test, so the connection
|
||||
// stays open and idle until Client is dropped
|
||||
let _ = client_drop_rx.wait();
|
||||
});
|
||||
|
||||
let uri = format!("http://{}/a", addr).parse().unwrap();
|
||||
|
||||
let res = {
|
||||
let client = Client::configure()
|
||||
.connector(DebugConnector(HttpConnector::new(1, &handle), closes.clone()))
|
||||
.no_proto()
|
||||
.build(&handle);
|
||||
client.get(uri)
|
||||
};
|
||||
|
||||
//let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
|
||||
core.run(res.select2(rx1)).unwrap();
|
||||
// res now dropped
|
||||
core.run(Timeout::new(Duration::from_millis(100), &handle).unwrap()).unwrap();
|
||||
|
||||
assert_eq!(closes.load(Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_response_body_closes_in_progress_connection() {
|
||||
let _ = pretty_env_logger::init();
|
||||
|
||||
let server = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let addr = server.local_addr().unwrap();
|
||||
let mut core = Core::new().unwrap();
|
||||
let handle = core.handle();
|
||||
let closes = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
let (tx1, rx1) = oneshot::channel();
|
||||
let (_client_drop_tx, client_drop_rx) = oneshot::channel::<()>();
|
||||
|
||||
thread::spawn(move || {
|
||||
let mut sock = server.accept().unwrap().0;
|
||||
sock.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
|
||||
sock.set_write_timeout(Some(Duration::from_secs(5))).unwrap();
|
||||
let mut buf = [0; 4096];
|
||||
sock.read(&mut buf).expect("read 1");
|
||||
write!(sock, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n").expect("write head");
|
||||
let _ = tx1.send(());
|
||||
|
||||
// prevent this thread from closing until end of test, so the connection
|
||||
// stays open and idle until Client is dropped
|
||||
let _ = client_drop_rx.wait();
|
||||
});
|
||||
|
||||
let uri = format!("http://{}/a", addr).parse().unwrap();
|
||||
|
||||
let res = {
|
||||
let client = Client::configure()
|
||||
.connector(DebugConnector(HttpConnector::new(1, &handle), closes.clone()))
|
||||
.no_proto()
|
||||
.build(&handle);
|
||||
// notably, havent read body yet
|
||||
client.get(uri)
|
||||
};
|
||||
|
||||
let rx = rx1.map_err(|_| hyper::Error::Io(io::Error::new(io::ErrorKind::Other, "thread panicked")));
|
||||
core.run(res.join(rx).map(|r| r.0)).unwrap();
|
||||
core.run(Timeout::new(Duration::from_millis(100), &handle).unwrap()).unwrap();
|
||||
|
||||
assert_eq!(closes.load(Ordering::Relaxed), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_keep_alive_closes_connection() {
|
||||
// https://github.com/hyperium/hyper/issues/1383
|
||||
|
||||
Reference in New Issue
Block a user