Skip to content

Commit 9aa7e99

Browse files
committed
fix(http2): revert http2 refactor causing a client hang
This reverts commit 7b7dcc8.
1 parent 078ed82 commit 9aa7e99

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ futures = "0.1.21"
2323
futures-cpupool = { version = "0.1.6", optional = true }
2424
http = "0.1.15"
2525
httparse = "1.0"
26-
h2 = "0.1.15"
26+
h2 = "0.1.10"
2727
iovec = "0.1"
2828
itoa = "0.4.1"
2929
log = "0.4"
@@ -52,6 +52,7 @@ serde_json = "1.0"
5252

5353
[features]
5454
default = [
55+
"__internal_flaky_tests",
5556
"runtime",
5657
]
5758
runtime = [
@@ -65,6 +66,7 @@ runtime = [
6566
"tokio-timer",
6667
]
6768
nightly = []
69+
__internal_flaky_tests = []
6870
__internal_happy_eyeballs_tests = []
6971

7072
[profile.release]

src/proto/h2/client.rs

+42-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
use bytes::IntoBuf;
22
use futures::{Async, Future, Poll, Stream};
3+
use futures::future::{self, Either};
4+
use futures::sync::mpsc;
35
use h2::client::{Builder, Handshake, SendRequest};
46
use tokio_io::{AsyncRead, AsyncWrite};
57

68
use headers::content_length_parse_all;
79
use body::Payload;
8-
use ::common::Exec;
10+
use ::common::{Exec, Never};
911
use headers;
1012
use ::proto::Dispatched;
1113
use super::{PipeToSendStream, SendBuf};
1214
use ::{Body, Request, Response};
1315

1416
type ClientRx<B> = ::client::dispatch::Receiver<Request<B>, Response<Body>>;
17+
/// An mpsc channel is used to help notify the `Connection` task when *all*
18+
/// other handles to it have been dropped, so that it can shutdown.
19+
type ConnDropRef = mpsc::Sender<Never>;
1520

1621
pub(crate) struct Client<T, B>
1722
where
@@ -24,7 +29,7 @@ where
2429

2530
enum State<T, B> where B: IntoBuf {
2631
Handshaking(Handshake<T, B>),
27-
Ready(SendRequest<B>),
32+
Ready(SendRequest<B>, ConnDropRef),
2833
}
2934

3035
impl<T, B> Client<T, B>
@@ -59,13 +64,40 @@ where
5964
let next = match self.state {
6065
State::Handshaking(ref mut h) => {
6166
let (request_tx, conn) = try_ready!(h.poll().map_err(::Error::new_h2));
67+
// An mpsc channel is used entirely to detect when the
68+
// 'Client' has been dropped. This is to get around a bug
69+
// in h2 where dropping all SendRequests won't notify a
70+
// parked Connection.
71+
let (tx, rx) = mpsc::channel(0);
72+
let rx = rx.into_future()
73+
.map(|(msg, _)| match msg {
74+
Some(never) => match never {},
75+
None => (),
76+
})
77+
.map_err(|_| -> Never { unreachable!("mpsc cannot error") });
6278
let fut = conn
6379
.inspect(|_| trace!("connection complete"))
64-
.map_err(|e| debug!("connection error: {}", e));
80+
.map_err(|e| debug!("connection error: {}", e))
81+
.select2(rx)
82+
.then(|res| match res {
83+
Ok(Either::A(((), _))) |
84+
Err(Either::A(((), _))) => {
85+
// conn has finished either way
86+
Either::A(future::ok(()))
87+
},
88+
Ok(Either::B(((), conn))) => {
89+
// mpsc has been dropped, hopefully polling
90+
// the connection some more should start shutdown
91+
// and then close
92+
trace!("send_request dropped, starting conn shutdown");
93+
Either::B(conn)
94+
}
95+
Err(Either::B((never, _))) => match never {},
96+
});
6597
self.executor.execute(fut)?;
66-
State::Ready(request_tx)
98+
State::Ready(request_tx, tx)
6799
},
68-
State::Ready(ref mut tx) => {
100+
State::Ready(ref mut tx, ref conn_dropper) => {
69101
try_ready!(tx.poll_ready().map_err(::Error::new_h2));
70102
match self.rx.poll() {
71103
Ok(Async::Ready(Some((req, mut cb)))) => {
@@ -98,6 +130,11 @@ where
98130
match pipe.poll() {
99131
Ok(Async::Ready(())) | Err(()) => (),
100132
Ok(Async::NotReady) => {
133+
let conn_drop_ref = conn_dropper.clone();
134+
let pipe = pipe.then(move |x| {
135+
drop(conn_drop_ref);
136+
x
137+
});
101138
self.executor.execute(pipe)?;
102139
}
103140
}

tests/integration.rs

+3
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ t! {
333333
;
334334
}
335335

336+
// In rare cases, the h2 client connection does not shutdown, resulting
337+
// in this test simply hanging... :(
338+
#[cfg(feature = "__internal_flaky_tests")]
336339
t! {
337340
http2_parallel_10,
338341
parallel: 0..10

0 commit comments

Comments
 (0)