Skip to content

fix(mux-h2): give back the send credit of DATA prepared and dropped unsent - #1646

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-send-window-refund
Sep 28, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-send-window-refund

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

H2WritePhase::Prepare (ConnectionH2::poll_write_target, lib/src/protocol/mux/h2.rs) debits the stream's and the connection's send windows when it encodes DATA, before the socket takes any of it. When the parked stream holding such DATA was removed or had its kawa.out cleared, the frames were dropped but the credit was never given back, and the peer never returns credit for DATA it never received (RFC 9113 §6.9). Each drop shrank the connection window for the life of the connection, until every stream's DATA stalled.

Scope, per the comment on #1631/#1632: a backend H2 connection belongs to one Mux session (Mux::router), so the damage stays within one session, but a long-lived frontend or backend connection could still starve.

Fix

Refund what will never be sent, rather than move the debit to transmit time (the converter needs the window as its budget at prepare time, so send-time accounting would still need an in-flight counter, and would walk frames on every write instead of on a park).

  • ConnectionH2::parked_data records, where a pass parks a stream, the DATA payload kawa.out still holds whole (h2_transmit::queued_data_payload). It shares the frame walk of h2_transmit::holds_header_frame (h2: HPACK encoder table diverges when an encoded HEADERS block is dropped unsent #1627), runs only on a park, and allocates nothing. The rest of a frame a partial write cut is in H2Output (fix(mux-h2): send every committed frame whole through one ordered output queue #1625) and is sent whole, so it is not counted.
  • ConnectionH2::remove_dead_stream, which every removal goes through, gives the parked credit back to the connection window before H2StreamTable::remove clears the park, and re-arms WRITABLE when that reopens a closed window.
  • The next H2WritePhase::Start gives back, to both windows, the part of a live park's DATA that is no longer queued (forcefully_terminate_answer, a default answer).
  • H2FlowControl::refund_send_window uses checked_add and asserts that the window grows by exactly the refund. A queue that ends inside a DATA frame does not count that frame, so the connection gives back too little rather than too much.
  • A peer can push window + unsent octets past 2^31-1 with WINDOW_UPDATEs that are each valid on their own and are sent while the DATA sits parked. A refund that would overflow is therefore peer input. It is not applied, and the connection answers GOAWAY(FLOW_CONTROL_ERROR) exactly as ConnectionH2::handle_window_update_frame does for an overflowing increment (RFC 9113 §6.9.1). Nothing asserts on it.

Prior art: hyperium/h2 Prioritize::reclaim_reserved_capacity returns reserved but unsent capacity on a reset. HAProxy debits h2s->sws/h2c->mws when a DATA frame enters the connection's mbuf, which is never dropped per stream.

Tests (red on 5db53c60, green here)

  • data_dropped_with_its_parked_stream_gives_the_connection_window_back: a 66535-octet body takes the whole connection window in one prepare, the socket takes 0 bytes, and the peer's RST_STREAM(1) removes the stream. On main the window stays at 0 and stream 3's 1000-octet body never leaves. The test also removes WRITABLE interest before the RST and asserts that the refund re-arms it (checked by mutation).
  • data_dropped_from_a_live_parked_stream_gives_both_windows_back: the same park, with forcefully_terminate_answer in place of the removal. On main both windows stay at 0.
  • data_a_partial_write_started_is_sent_whole_and_not_given_back: the socket cuts the first DATA frame. The frame still leaves whole (16384 octets), and window + DATA sent == 65535. On main this reads 16384 against 65535. If the whole prepare is refunded instead, it reads 81919.
  • data_still_parked_after_a_second_stalled_pass_is_not_given_back: a second stalled pass keeps the park and must give nothing back. If parked_data - queued becomes parked_data, the window reads 65535 against 0.
  • a_refund_the_peer_pushed_past_the_window_ceiling_is_a_flow_control_error: stream 1 is parked, then the peer sends WINDOW_UPDATE(0, 2^31-1) and RST_STREAM(1). On 08730c9a this hit the debug_assert! (a panic caused by network input). The connection now sends exactly one GOAWAY carrying FLOW_CONTROL_ERROR. If the overflow is dropped silently instead, no GOAWAY is sent (left: 0, right: 1).
  • Unit tests: queued_data_payload_counts_only_data_payload_octets, refund_gives_back_consumed_credit_and_reports_a_reopened_window and a_refund_past_the_window_ceiling_is_an_overflow_and_changes_nothing.

The harness runs in Position::Server. The Start/Flush arms and remove_dead_stream never branch on position, so the backend path is the same code. Each TO SEE THIS RED recipe was checked by mutation.

Commands

  • cargo build --locked, cargo build --all-features --locked
  • cargo clippy --all-targets --locked -- -D warnings, plus the same with --no-default-features --features crypto-ring,opentelemetry,splice,simd
  • cargo +nightly fmt --all -- --check
  • cargo test --workspace --locked -- --skip tests::fuzz_tests:: --test-threads=8
  • RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked
  • check_doc_citations.py: --self-test, the fixture root (exits 1 as expected), plain, and --base origin/main

The Store::Static fixture works around CleverCloud/kawa#22 (Store::consume overflows on a split Store::Alloc in debug builds).

Docs

lib/src/protocol/mux/LIFECYCLE.md (§5.1 and a new invariant 28 property), doc/h2_mux_internals.md (new subsection "Prepared DATA dropped unsent gives its send credit back"), and CHANGELOG.md (Fixed). Line citations into h2.rs were re-anchored, because adding the production code shifted them.

Closes #1641

Signed-off-by: Florentin Dubois [email protected]

…nsent

The write pass debits the stream's and the connection's send windows when
`H2WritePhase::Prepare` encodes DATA into `kawa.out`, before the socket
takes any of it. RFC 9113 §6.9 counts only DATA that was sent, and the
peer returns credit through WINDOW_UPDATE only for what it received. When
the parked stream holding such DATA was removed (peer RST_STREAM, client
cancel, timeout, `prune_inactive_streams_while_closing`) or had its queue
cleared (`forcefully_terminate_answer`, a default answer), the frames were
dropped and the credit was lost for the life of the connection. Enough
drops closed the connection window, every stream's DATA stalled, and the
flow-control-stall reaper cancelled streams that had done nothing wrong.

Only the stream parked in `expect_write` holds unsent frames, the argument
now records the DATA payload `kawa.out` still holds whole
(`h2_transmit::queued_data_payload`, the frame walk
`holds_header_frame` already used, now shared, with no allocation). The
rest of a frame a partial write cut is not counted: `H2Output` adopted it
and sends it whole (#1625). `ConnectionH2::remove_dead_stream` gives that
credit back to the connection window when it removes the parked stream,
re-arming WRITABLE when the window reopens, and the next
`H2WritePhase::Start` gives back to both windows whatever a live park no
longer queues. `H2FlowControl::refund_send_window` asserts the window
grows by exactly the refund; a queue that ends inside a DATA frame does
not count it, so the connection gives back too little rather than too
much. A refund can still pass 2^31-1 when the peer's WINDOW_UPDATEs,
each valid on its own, overgranted while the DATA sat parked: that is
peer input, so it is not applied and the connection answers
GOAWAY(FLOW_CONTROL_ERROR), as for an overflowing increment
(RFC 9113 §6.9.1), rather than asserting.

Refunding the unsent part matches hyperium/h2, whose `Prioritize`
reclaims reserved but unsent capacity on a reset
(`reclaim_reserved_capacity`). HAProxy debits `h2s->sws`/`h2c->mws` when
a frame enters the connection's `mbuf`, which a stream never drops.
Debiting at transmit time instead would still need an in-flight counter,
because the converter uses the window as its budget at prepare time, and
would walk frames on every write rather than on a park.

Three connection-level tests, red on 5db53c6: a parked window-sized body
removed by the peer's RST_STREAM (the window stayed 0 and stream 3's body
never left), a live parked stream whose queue a forced termination
cleared (both windows stayed 0), and a partial write whose cut frame is
sent whole while only the frames behind it are given back. Two more
cover a second stalled pass, which must give nothing back, and a peer
that overgrants before resetting the parked stream, which panicked on a
debug assertion before and now gets GOAWAY(FLOW_CONTROL_ERROR).

Closes #1641

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h2-send-window-refund branch from db29d26 to f36bf77 Compare September 28, 2026 05:12
@FlorentinDUBOIS
FlorentinDUBOIS merged commit a3d3dd4 into main Sep 28, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h2-send-window-refund branch September 28, 2026 06:22
FlorentinDUBOIS added a commit that referenced this pull request Nov 12, 2026
…UATION block (#1923)

* refactor(mux-h2): drop the unreachable refusal of an oversized CONTINUATION block

`ConnectionH2::handle_continuation_header_state` held a branch that
refused a header block whose CONTINUATION frames carried it past
`max_header_list_size` with RST_STREAM(REFUSED_STREAM). It could not
run. `check_flood_or_return!` sits right before it and calls
`H2FloodDetector::check_flood`, whose `accumulated header size` check is
the same strict `>` on the same two values
(`H2FloodDetector::accumulated_header_size` and
`H2FloodConfig::max_header_list_size`), and no path of that function or
of `ConnectionH2::handle_flood_violation` lets such a frame through:
`maybe_reset_window` touches only the rate counters, and every violation
ends in `ConnectionH2::goaway`, for a frontend and a backend connection
alike. The oversized block therefore ends the connection with
GOAWAY(ENHANCE_YOUR_CALM), which
`an_oversized_header_block_ends_the_connection_without_refusing_its_stream`
pins on both sides.

Had it run, the branch would have dropped the stream's wire id with its
slot not recycled, the state `ConnectionH2::check_slot_ownership`
forbids.

The branch is replaced by a `debug_assert!` stating what the flood check
guarantees: a CONTINUATION that got past it leaves the accumulated size
within `max_header_list_size`. The check that follows, a payload larger
than the room left in `zero.storage` ending the connection, is
unchanged, and release behaviour is unchanged.

Nothing else became dead: `refuse_stream_and_discard` keeps its three
callers, which now only pass `DiscardedFieldBlock::New` and
`RstOrigin::Local`; `DiscardedFieldBlock::Continuation` is still built
for the CONTINUATION of a block that is already being discarded
(`ConnectionH2::handle_header_state`); and `HeaderBlockAccumulator::finish`
keeps its callers in `ConnectionH2::handle_headers_frame`, though no
production caller reads the bytes it returns any more. Both are left as
they are. The prose that described the refusal as live
(`doc/h2_mux_internals.md`, `doc/testing.md`, the mux `LIFECYCLE.md`, the
`ConfigError::H2ThresholdBelowMinimum` and `H2FloodConfig::from_optional`
docs, the `DiscardedFieldBlock` and accumulator docs) now says what is
true, and the pinned `h2.rs` blocks of `doc/h2_mux_internals.md` follow the
lines the deletion moved.

The test's red recipe no longer describes the removed branch: deleting
the `check_flood_or_return!` now trips the new `debug_assert!`, and,
with that deleted as well, the test's own `got Header` assertion. The
test is renamed because nothing follows the end of the connection any
more.

Signed-off-by: Florentin Dubois <[email protected]>

* test(mux-h2): state the failure the adopt_tail mutation produces now

The "TO SEE THIS RED" recipes of the half-written-frame tests name the
mutation that replaces `self.output.adopt_tail(kawa, tail);` in
`ConnectionH2::handle_write` by `let _ = (kawa, tail);`, and quote the
assertions that fail under it. They no longer fail where the recipes say.
The `an H2 stream queues whole frames only` debug assertion of
`h2_transmit::queued_data_payload`, added with the send credit given back
for DATA dropped unsent (#1646), fires first in every test on the
`ack_after_half_written_frame` fixture, so none reaches
`h2_transmit::frame_tail`'s `a frame runs past the gathered bytes` or its
own wire assertion.

The recipes now say so. They also record that the same mutation, with
debug assertions off (`CARGO_PROFILE_TEST_DEBUG_ASSERTIONS=false`),
reaches the wire assertions, and that the removed-stream and peer-reset
tests then fail with the three `frame type ... remain` messages they have
always quoted. `a_deferred_ack_does_not_strand_reads_when_its_parked_stream_is_removed`
passes in that configuration.

Comments only: no assertion, fixture or production line changes.

Signed-off-by: Florentin Dubois <[email protected]>

---------

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Nov 16, 2026
…UATION block (#1925)

* refactor(mux-h2): drop the unreachable refusal of an oversized CONTINUATION block

`ConnectionH2::handle_continuation_header_state` held a branch that
refused a header block whose CONTINUATION frames carried it past
`max_header_list_size` with RST_STREAM(REFUSED_STREAM). It could not
run. `check_flood_or_return!` sits right before it and calls
`H2FloodDetector::check_flood`, whose `accumulated header size` check is
the same strict `>` on the same two values
(`H2FloodDetector::accumulated_header_size` and
`H2FloodConfig::max_header_list_size`), and no path of that function or
of `ConnectionH2::handle_flood_violation` lets such a frame through:
`maybe_reset_window` touches only the rate counters, and every violation
ends in `ConnectionH2::goaway`, for a frontend and a backend connection
alike. The oversized block therefore ends the connection with
GOAWAY(ENHANCE_YOUR_CALM), which
`an_oversized_header_block_ends_the_connection_without_refusing_its_stream`
pins on both sides.

Had it run, the branch would have dropped the stream's wire id with its
slot not recycled, the state `ConnectionH2::check_slot_ownership`
forbids.

The branch is replaced by a `debug_assert!` stating what the flood check
guarantees: a CONTINUATION that got past it leaves the accumulated size
within `max_header_list_size`. The check that follows, a payload larger
than the room left in `zero.storage` ending the connection, is
unchanged, and release behaviour is unchanged.

Nothing else became dead: `refuse_stream_and_discard` keeps its three
callers, which now only pass `DiscardedFieldBlock::New` and
`RstOrigin::Local`; `DiscardedFieldBlock::Continuation` is still built
for the CONTINUATION of a block that is already being discarded
(`ConnectionH2::handle_header_state`); and `HeaderBlockAccumulator::finish`
keeps its callers in `ConnectionH2::handle_headers_frame`, though no
production caller reads the bytes it returns any more. Both are left as
they are. The prose that described the refusal as live
(`doc/h2_mux_internals.md`, `doc/testing.md`, the mux `LIFECYCLE.md`, the
`ConfigError::H2ThresholdBelowMinimum` and `H2FloodConfig::from_optional`
docs, the `DiscardedFieldBlock` and accumulator docs) now says what is
true, and the pinned `h2.rs` blocks of `doc/h2_mux_internals.md` follow the
lines the deletion moved.

The test's red recipe no longer describes the removed branch: deleting
the `check_flood_or_return!` now trips the new `debug_assert!`, and,
with that deleted as well, the test's own `got Header` assertion. The
test is renamed because nothing follows the end of the connection any
more.

Signed-off-by: Florentin Dubois <[email protected]>

* test(mux-h2): state the failure the adopt_tail mutation produces now

The "TO SEE THIS RED" recipes of the half-written-frame tests name the
mutation that replaces `self.output.adopt_tail(kawa, tail);` in
`ConnectionH2::handle_write` by `let _ = (kawa, tail);`, and quote the
assertions that fail under it. They no longer fail where the recipes say.
The `an H2 stream queues whole frames only` debug assertion of
`h2_transmit::queued_data_payload`, added with the send credit given back
for DATA dropped unsent (#1646), fires first in every test on the
`ack_after_half_written_frame` fixture, so none reaches
`h2_transmit::frame_tail`'s `a frame runs past the gathered bytes` or its
own wire assertion.

The recipes now say so. They also record that the same mutation, with
debug assertions off (`CARGO_PROFILE_TEST_DEBUG_ASSERTIONS=false`),
reaches the wire assertions, and that the removed-stream and peer-reset
tests then fail with the three `frame type ... remain` messages they have
always quoted. `a_deferred_ack_does_not_strand_reads_when_its_parked_stream_is_removed`
passes in that configuration.

Comments only: no assertion, fixture or production line changes.

Signed-off-by: Florentin Dubois <[email protected]>

---------

Signed-off-by: Florentin Dubois <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

h2: send window debited for DATA prepared but never sent is not credited back when the stream is removed

1 participant