Skip to content

Commit ab3c73f

Browse files
committed
fix(http2): force notify h2 client connection when all body streams drop
1 parent d3e4089 commit ab3c73f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/proto/h2/client.rs

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bytes::IntoBuf;
22
use futures::{Async, Future, Poll, Stream};
33
use futures::future::{self, Either};
4-
use futures::sync::oneshot;
4+
use futures::sync::mpsc;
55
use h2::client::{Builder, Handshake, SendRequest};
66
use tokio_io::{AsyncRead, AsyncWrite};
77

@@ -11,6 +11,9 @@ use super::{PipeToSendStream, SendBuf};
1111
use ::{Body, Request, Response};
1212

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

1518
pub struct Client<T, B>
1619
where
@@ -23,7 +26,7 @@ where
2326

2427
enum State<T, B> where B: IntoBuf {
2528
Handshaking(Handshake<T, B>),
26-
Ready(SendRequest<B>, oneshot::Sender<Never>),
29+
Ready(SendRequest<B>, ConnDropRef),
2730
}
2831

2932
impl<T, B> Client<T, B>
@@ -58,11 +61,17 @@ where
5861
let next = match self.state {
5962
State::Handshaking(ref mut h) => {
6063
let (request_tx, conn) = try_ready!(h.poll().map_err(::Error::new_h2));
61-
// A oneshot channel is used entirely to detect when the
64+
// An mpsc channel is used entirely to detect when the
6265
// 'Client' has been dropped. This is to get around a bug
6366
// in h2 where dropping all SendRequests won't notify a
6467
// parked Connection.
65-
let (tx, rx) = oneshot::channel();
68+
let (tx, rx) = mpsc::channel(0);
69+
let rx = rx.into_future()
70+
.map(|(msg, _)| match msg {
71+
Some(never) => match never {},
72+
None => (),
73+
})
74+
.map_err(|_| -> Never { unreachable!("mpsc cannot error") });
6675
let fut = conn
6776
.inspect(|_| trace!("connection complete"))
6877
.map_err(|e| debug!("connection error: {}", e))
@@ -73,19 +82,19 @@ where
7382
// conn has finished either way
7483
Either::A(future::ok(()))
7584
},
76-
Err(Either::B((_, conn))) => {
77-
// oneshot has been dropped, hopefully polling
85+
Ok(Either::B(((), conn))) => {
86+
// mpsc has been dropped, hopefully polling
7887
// the connection some more should start shutdown
7988
// and then close
8089
trace!("send_request dropped, starting conn shutdown");
8190
Either::B(conn)
8291
}
83-
Ok(Either::B((never, _))) => match never {},
92+
Err(Either::B((never, _))) => match never {},
8493
});
8594
self.executor.execute(fut);
8695
State::Ready(request_tx, tx)
8796
},
88-
State::Ready(ref mut tx, _) => {
97+
State::Ready(ref mut tx, ref conn_dropper) => {
8998
try_ready!(tx.poll_ready().map_err(::Error::new_h2));
9099
match self.rx.poll() {
91100
Ok(Async::Ready(Some((req, mut cb)))) => {
@@ -107,8 +116,14 @@ where
107116
}
108117
};
109118
if !eos {
110-
let pipe = PipeToSendStream::new(body, body_tx);
111-
self.executor.execute(pipe.map_err(|e| debug!("client request body error: {}", e)));
119+
let conn_drop_ref = conn_dropper.clone();
120+
let pipe = PipeToSendStream::new(body, body_tx)
121+
.map_err(|e| debug!("client request body error: {}", e))
122+
.then(move |x| {
123+
drop(conn_drop_ref);
124+
x
125+
});
126+
self.executor.execute(pipe);
112127
}
113128

114129
let fut = fut

0 commit comments

Comments
 (0)