Skip to content

Commit

Permalink
Fix reclaiming reserved capacity (#832)
Browse files Browse the repository at this point in the history
Reclaiming requested capacity that has not been actually reserved yet
is wrong, as this capacity never existed to begin with.

Fixes #607
  • Loading branch information
nox authored Jan 20, 2025
1 parent d7c56f4 commit b109803
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/proto/streams/prioritize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,18 @@ impl Prioritize {
/// Reclaim just reserved capacity, not buffered capacity, and re-assign
/// it to the connection
pub fn reclaim_reserved_capacity(&mut self, stream: &mut store::Ptr, counts: &mut Counts) {
// only reclaim requested capacity that isn't already buffered
if stream.requested_send_capacity as usize > stream.buffered_send_data {
let reserved = stream.requested_send_capacity - stream.buffered_send_data as WindowSize;
// only reclaim reserved capacity that isn't already buffered
if stream.send_flow.available().as_size() as usize > stream.buffered_send_data {
let reserved =
stream.send_flow.available().as_size() - stream.buffered_send_data as WindowSize;

// Panic safety: due to how `reserved` is computed it can't be greater
// than what's available.
stream
.send_flow
.claim_capacity(reserved)
.expect("window size should be greater than reserved");

// TODO: proper error handling
let _res = stream.send_flow.claim_capacity(reserved);
debug_assert!(_res.is_ok());
self.assign_connection_capacity(reserved, stream, counts);
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/h2-support/src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ impl Mock<frame::Settings> {
self
}

pub fn max_frame_size(mut self, val: u32) -> Self {
self.0.set_max_frame_size(Some(val));
self
}

pub fn initial_window_size(mut self, val: u32) -> Self {
self.0.set_initial_window_size(Some(val));
self
Expand Down
93 changes: 93 additions & 0 deletions tests/h2-tests/tests/flow_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,3 +1898,96 @@ async fn window_size_decremented_past_zero() {

join(client, srv).await;
}

#[tokio::test]
async fn reclaim_reserved_capacity() {
use futures::channel::oneshot;

h2_support::trace_init!();

let (io, mut srv) = mock::new();
let (depleted_tx, depleted_rx) = oneshot::channel();

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

srv.recv_frame(frames::headers(1).request("POST", "https://www.example.com/"))
.await;
srv.send_frame(frames::headers(1).response(200)).await;

srv.recv_frame(frames::data(1, vec![0; 16384])).await;
srv.recv_frame(frames::data(1, vec![0; 16384])).await;
srv.recv_frame(frames::data(1, vec![0; 16384])).await;
srv.recv_frame(frames::data(1, vec![0; 16383])).await;
depleted_tx.send(()).unwrap();

// By now, this peer's connection window is completely depleted.

srv.recv_frame(frames::headers(3).request("POST", "https://www.example.com/"))
.await;
srv.send_frame(frames::headers(3).response(200)).await;

srv.recv_frame(frames::reset(1).cancel()).await;
};

let h2 = async move {
let (mut client, mut h2) = client::handshake(io).await.unwrap();

let mut depleting_stream = {
let request = Request::builder()
.method(Method::POST)
.uri("https://www.example.com/")
.body(())
.unwrap();

let (resp, stream) = client.send_request(request, false).unwrap();

{
let resp = h2.drive(resp).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}

stream
};

depleting_stream
.send_data(vec![0; 65535].into(), false)
.unwrap();
h2.drive(depleted_rx).await.unwrap();

// By now, the client knows it has completely depleted the server's
// connection window.

depleting_stream.reserve_capacity(1);

let mut starved_stream = {
let request = Request::builder()
.method(Method::POST)
.uri("https://www.example.com/")
.body(())
.unwrap();

let (resp, stream) = client.send_request(request, false).unwrap();

{
let resp = h2.drive(resp).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}

stream
};

// The following call puts starved_stream in pending_send, as the
// server's connection window is completely empty.
starved_stream.send_data(vec![0; 1].into(), false).unwrap();

// This drop should change nothing, as it didn't actually reserve
// any available connection window, only requested it.
drop(depleting_stream);

h2.await.unwrap();
};

join(mock, h2).await;
}

0 comments on commit b109803

Please sign in to comment.