Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/proto/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ where
let _e = self.inner.span.enter();
let span = tracing::trace_span!("poll_ready");
let _e = span.enter();
// After receiving a peer GOAWAY, flush any locally opened streams
// already queued in the send buffer before sending a PING ACK so a
// server performing RFC 9113 §6.8 two-step shutdown observes those
// streams before committing its final GOAWAY last_stream_id.
if self.inner.error.is_some() && self.inner.ping_pong.has_pending_pong() {
ready!(self.inner.streams.poll_complete(cx, &mut self.codec))?;
}
// The order of these calls don't really matter too much
ready!(self.inner.ping_pong.send_pending_pong(cx, &mut self.codec))?;
ready!(self.inner.ping_pong.send_pending_ping(cx, &mut self.codec))?;
Expand Down
5 changes: 5 additions & 0 deletions src/proto/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ impl PingPong {
}
}

/// Returns whether a pong frame is waiting to be sent.
pub(crate) fn has_pending_pong(&self) -> bool {
self.pending_pong.is_some()
}

/// Send any pending pongs.
pub(crate) fn send_pending_pong<T, B>(
&mut self,
Expand Down
58 changes: 58 additions & 0 deletions tests/h2-tests/tests/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,61 @@ async fn user_notifies_when_connection_closes() {
"broken pipe",
);
}

#[tokio::test]
async fn queued_request_flushes_before_shutdown_ping_ack() {
h2_support::trace_init!();
let (io, mut srv) = mock::new();

let (ready_tx, ready_rx) = oneshot::channel();
let (sent_tx, sent_rx) = oneshot::channel();

let srv = async move {
let settings = srv.assert_client_handshake().await;
assert_default_settings!(settings);

// Buffer GOAWAY(2^31-1) + PING into the pipe before the client polls
// the connection again, while the client queues a request in its send
// buffer. Per RFC 9113 §6.8, the server will commit last_stream_id on
// the PING ACK, so any stream already queued before GOAWAY was
// processed must arrive before the PING ACK.
srv.send_frame(frames::go_away(StreamId::MAX)).await;
srv.send_frame(frames::ping(frame::Ping::SHUTDOWN)).await;
ready_tx.send(()).unwrap();
sent_rx.await.unwrap();

srv.recv_frame(
frames::headers(1)
.request("POST", "https://http2.akamai.com/")
.eos(),
)
.await;
srv.recv_frame(frames::ping(frame::Ping::SHUTDOWN).pong())
.await;
srv.send_frame(frames::go_away(1)).await;
srv.send_frame(frames::headers(1).response(200).eos()).await;
srv.recv_frame(frames::go_away(0)).await;
srv.recv_eof().await;
};

let client = async move {
let (mut client, mut conn) = client::handshake(io).await.expect("client handshake");
conn.drive(util::yield_once()).await;

ready_rx.await.unwrap();
let req = Request::builder()
.method(Method::POST)
.uri("https://http2.akamai.com/")
.body(())
.unwrap();
let (resp, _) = client.send_request(req, true).expect("send_request");
sent_tx.send(()).unwrap();

let resp = conn.drive(resp).await.expect("response");
assert_eq!(resp.status(), StatusCode::OK);
drop(client);
conn.await.expect("client");
};

join(srv, client).await;
}
Loading