Skip to content

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

Closed

Description

@FlorentinDUBOIS

Summary

The connection-level send window of an H2 connection is debited when DATA frames are prepared (kawa.prepare), not when they reach the wire. When a stream holding prepared, unsent DATA frames is removed, those frames are dropped, but nothing gives the credit back. The peer never received those bytes, so it never returns them with a WINDOW_UPDATE either (RFC 9113 §6.9.1: the window accounts for DATA frames that were sent). The connection window therefore shrinks permanently, once for every such removal. On a backend connection that the router shares between clients, the window can eventually reach zero, at which point every request body on that connection stalls.

Observed on 6eef0ba1, by code reading; no reproduction test yet.

Mechanism

  • In the H2WritePhase::Prepare arm of ConnectionH2::poll_write_target (lib/src/protocol/mux/h2.rs), the pass encodes up to window = min(stream window, connection window) bytes of DATA into kawa.out. It then debits both windows for what was encoded (pass.consumed): *parts.window and self.flow_control.consume_send_window(pass.consumed) (H2FlowControl::consume_send_window, lib/src/protocol/mux/h2_flow_control.rs). No byte has been written at that point.
  • When the socket takes 0 bytes, the pass parks the stream (expect_write) with those frames still in kawa.out. When it takes part of a frame, only the rest of that one frame is adopted into the ordered output (H2Output::adopt_tail); the whole frames behind it stay in kawa.out.
  • If the stream is removed while parked, the frames still in its kawa.out are never sent. Removal can come from ConnectionH2::end_stream (client cancel, backend timeout), a peer RST_STREAM (ConnectionH2::handle_rst_stream_frame), ConnectionH2::cancel_timed_out_streams, prune_inactive_streams_while_closing, or a stream recycled or reused afterwards. The same applies when forcefully_terminate_answer or a default answer clears the parked queue.
  • ConnectionH2::remove_dead_stream and the paths above never credit the connection window back. H2FlowControl has no refund operation; its window only grows through apply_window_update.
  • The peer sends WINDOW_UPDATE only for DATA it received. Credit for DATA that never left is therefore lost for the lifetime of the connection.

Scenario (backend connection, Position::Client)

  1. One H2 backend connection carries the uploads of several clients (Router::connect multiplexes them), with the default 65 535-byte connection window.
  2. The backend reads slowly, so the socket buffer fills. A client's request body is prepared as DATA (for example 16 KiB, which debits the connection window by 16 KiB), the write takes 0 bytes, and the stream is parked.
  3. That client gives up (browser navigation, fetch abort) or the stream times out. The stream is removed and its prepared DATA is dropped unsent.
  4. The 16 KiB are never credited back. After roughly four such cancellations the connection window is 0, and every other stream's body on this connection waits for a WINDOW_UPDATE that the backend will never send for bytes it never got. The flow-control-stall reaper then cancels those streams too, with no fault on their side.

The frontend position has the same shape, with response DATA toward the client, but there the damage stays within one client's own connection.

Expected

The connection (and stream) window accounts only for DATA that reached the wire, or is credited back for prepared DATA that is dropped. Options:

  • Credit H2FlowControl back with the DATA payload bytes still queued in a stream's kawa.out when that queue is dropped. The frame headers are already walked on a park (h2_transmit::holds_header_frame), so the same walk can sum the DATA lengths. Every site that removes a parked stream or clears its queue would need the refund.
  • Or debit the window at transmit time (h2_transmit::confirm) instead of prepare time (send-time accounting).

A regression test would need an H2 backend connection with a paced socket (0-byte first write), a DATA frame prepared and parked, the stream removed, and an assertion that flow_control.window() is back to its value before the prepare.

Activity

  1. added 2 commits that reference this issue on Sep 28, 2026
    db29d26
    f36bf77
  2. added a commit that references this issue on Sep 28, 2026
    a3d3dd4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions