Skip to content

Commit 48a0066

Browse files
committed
Keep the HTTP/2 receive window credit in sync when the window shrinks
Shrinking :initial_window_size adjusted each stream's receive window size but not its remaining credit. The server applies the difference to its view of the window as well (RFC 9113 6.9.2), so the next refill computed an increment of zero or a negative number and sent it in a WINDOW_UPDATE frame. A negative increment lands in the unsigned 31-bit field as a value near 2^31, which the server has to answer with a FLOW_CONTROL_ERROR stream reset (RFC 9113 6.9.1), and a zero increment is a PROTOCOL_ERROR stream error (RFC 9113 6.9). The remaining credit now follows the size change, and a refill is only sent when the increment is positive.
1 parent 9e9ae10 commit 48a0066

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎lib/mint/http2.ex‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,8 +1911,9 @@ defmodule Mint.HTTP2 do
19111911
end
19121912

19131913
defp maybe_refill_conn(frames, conn) do
1914-
if conn.receive_window_remaining <= conn.receive_window_update_threshold do
1915-
increment = conn.receive_window_size - conn.receive_window_remaining
1914+
increment = conn.receive_window_size - conn.receive_window_remaining
1915+
1916+
if conn.receive_window_remaining <= conn.receive_window_update_threshold and increment > 0 do
19161917
[window_update(stream_id: 0, window_size_increment: increment) | frames]
19171918
else
19181919
frames
@@ -1922,9 +1923,10 @@ defmodule Mint.HTTP2 do
19221923
defp maybe_refill_stream(frames, conn, stream_id) do
19231924
case Map.fetch(conn.streams, stream_id) do
19241925
{:ok, stream} ->
1925-
if stream.receive_window_remaining <= conn.receive_window_update_threshold do
1926-
increment = stream.receive_window_size - stream.receive_window_remaining
1926+
increment = stream.receive_window_size - stream.receive_window_remaining
19271927

1928+
if stream.receive_window_remaining <= conn.receive_window_update_threshold and
1929+
increment > 0 do
19281930
[
19291931
window_update(stream_id: stream_id, window_size_increment: increment) | frames
19301932
]
@@ -2255,9 +2257,12 @@ defmodule Mint.HTTP2 do
22552257
for {stream_id, stream} <- streams,
22562258
stream.state in [:open, :half_closed_local, :reserved_remote],
22572259
into: streams do
2258-
receive_window_size = stream.receive_window_size + diff
2259-
2260-
{stream_id, %{stream | receive_window_size: receive_window_size}}
2260+
{stream_id,
2261+
%{
2262+
stream
2263+
| receive_window_size: stream.receive_window_size + diff,
2264+
receive_window_remaining: stream.receive_window_remaining + diff
2265+
}}
22612266
end
22622267
end)
22632268

‎test/mint/http2/conn_test.exs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,40 @@ defmodule Mint.HTTP2Test do
22472247
end
22482248

22492249
describe "settings" do
2250+
@tag connect_options: [
2251+
receive_window_update_threshold: 8,
2252+
client_settings: [initial_window_size: 16]
2253+
]
2254+
test "shrinking the initial window size keeps the remaining credit in sync",
2255+
%{conn: conn} do
2256+
{conn, ref} = open_request(conn)
2257+
2258+
assert_recv_frames [headers(stream_id: stream_id)]
2259+
2260+
assert {:ok, %HTTP2{} = conn, _responses} =
2261+
stream_frames(conn, [
2262+
{:headers, stream_id, [{":status", "200"}], [:end_headers]},
2263+
data(stream_id: stream_id, data: "123456")
2264+
])
2265+
2266+
assert conn.streams[stream_id].receive_window_remaining == 10
2267+
2268+
{:ok, conn} = HTTP2.put_settings(conn, initial_window_size: 8)
2269+
assert_recv_frames [settings(params: [initial_window_size: 8])]
2270+
2271+
assert {:ok, %HTTP2{} = conn, []} =
2272+
stream_frames(conn, [settings(flags: set_flags(:settings, [:ack]), params: [])])
2273+
2274+
assert conn.streams[stream_id].receive_window_size == 8
2275+
assert conn.streams[stream_id].receive_window_remaining == 2
2276+
2277+
assert {:ok, %HTTP2{} = conn, [{:data, ^ref, "12"}]} =
2278+
stream_frames(conn, [data(stream_id: stream_id, data: "12")])
2279+
2280+
assert conn.streams[stream_id].receive_window_remaining == 8
2281+
assert_recv_frames [window_update(stream_id: ^stream_id, window_size_increment: 8)]
2282+
end
2283+
22502284
test "put_settings/2 can be used to send settings to server", %{conn: conn} do
22512285
{:ok, conn} =
22522286
HTTP2.put_settings(conn, max_concurrent_streams: 123, initial_window_size: 1_000)

0 commit comments

Comments
 (0)