Skip to content

Return the HTTP/2 connection window reserved by a stream that is reset before it writes - #1277

Open
vzhn wants to merge 1 commit into
apache:mainfrom
vzhn:http2-return-unused-connection-window
Open

vzhn wants to merge 1 commit into
apache:mainfrom
vzhn:http2-return-unused-connection-window

Conversation

@vzhn

@vzhn vzhn commented Nov 28, 2026

Copy link
Copy Markdown

StreamOutputBuffer.flush() reserves connection flow control window with Http2UpgradeHandler.reserveWindowSize()
and then calls writeBody(). writeBody() (sync and async) checks stream.canWrite() and sends nothing if the
stream can no longer write. If the client's RST_STREAM for that stream is processed between the two calls, the
reserved bytes are neither sent nor returned. A flow control window is reduced only by the flow-controlled frames
actually sent (RFC 9113, section 6.9.1), so the server's
view of the connection window becomes permanently smaller than the client's. The client has not received those bytes, so it does not send a WINDOW_UPDATE for them. Once enough
window has been lost, streams wait in the backlog for window the client believes it has already granted, until the
write timeout closes the connection.

sequenceDiagram
    participant W as Writer thread (StreamOutputBuffer.flush)
    participant H as Http2UpgradeHandler
    participant R as Reader thread (client frames)
    W->>H: reserveWindowSize(stream, n): stream can write, window -= n
    R->>H: RST_STREAM: stream.receiveReset()
    W->>H: writeBody(stream, data, n)
    Note over H: stream.canWrite() is false, nothing is sent
    Note over H: today: the n bytes are lost from the connection window
    H-->>W: writeBody() returns false
    Note over W: with this change: flush() calls releaseConnectionAllocation(n), window += n
Loading

Change

  • writeBody() (Http2UpgradeHandler and Http2AsyncUpgradeHandler) now returns whether it wrote the frame.
    StreamOutputBuffer.flush(), which made the reservation, releases it when writeBody() did not write, so the
    reservation and its release stay in one place.
  • New Http2UpgradeHandler.releaseConnectionAllocation(int) releases unused connection window through
    incrementWindowSize(), so streams waiting in the backlog are released as for a WINDOW_UPDATE. If the release would
    take the window above 2^31-1 (only possible with a client that has already pushed it to the maximum) it closes the
    connection, as the sendfile path did. The sendfile path (Http2AsyncUpgradeHandler.releaseReservation()) now uses
    it too, with no change in behaviour.

Locks: the release runs on the writer thread, which holds the stream's output buffer lock. incrementWindowSize()
takes the connection's windowAllocationLock, releases it, and then notifies waiting streams one at a time under
their own allocation locks. No thread takes an output buffer lock while holding an allocation lock, so this adds no
new lock ordering. If the reset stream is still in the backlog for the rest of a partial reservation, the released
window can be granted straight back to it; its next reserveWindowSize() fails without using it and
replaceStream() returns it when the stream is closed, as for any allocation a reset stream did not use.

Tests

TestHttp2ConnectionWindowReset makes the interleaving deterministic: a test Http2Protocol creates handlers whose
reserveWindowSize() calls stream.receiveReset(CANCEL) (what processing the client's RST_STREAM does) as soon as
it has allocated window to the stream under test, i.e. just before that stream writes. A servlet writes and flushes
the body. Both tests run with sync and async I/O.

test window allocated without the change with the change
testResetAfterConnectionWindowReserved directly from the connection window fails: expected:<57343> but was:<56319> (1,024 bytes lost) passes
testResetAfterConnectionWindowAllocatedFromBacklog from the backlog: stream 3 uses the whole window, stream 5 waits, a 1,024-byte WINDOW_UPDATE is allocated to it fails: expected:<1024> but was:<0> (the whole update lost) passes

All org.apache.coyote.http2 tests pass with the change (7,488 tests, sync and async I/O), and Checkstyle
(ant validate) passes.

How it was found and reproduced

Found with a TLA+ model of the connection flow control code (reserveWindowSize, incrementWindowSize,
releaseBackLog, allocate, replaceStream). The model reproduces the over-allocation fixed by 0dcc63b and the
lost rounding remainder fixed by fc41b15 on their earlier code, finds this leak on main, and, with a client that
sends WINDOW_UPDATE only after receiving everything it granted, a stall. With this change it finds neither, also when
the reset, the skipped write with its release, and replaceStream() are separate steps (so released window can be
granted back to the reset stream before replaceStream() returns it).

On real Tomcat (main 11f173c, embedded, NIO, h2c) a raw HTTP/2 client ran 3,000 rounds of a 4 KB WINDOW_UPDATE
followed, after 0-100 us, by RST_STREAM of the stream first in line for it. It then waited 8 s and opened a stream
asking for more than the client's view of the connection window:

build I/O window lost probe stream
main async 9,216 bytes 0 bytes received (stalled)
main sync 13,312 bytes 0 bytes received (stalled)
main, no resets (control) both 0 all 11,698,175 bytes
this change async / sync 0 (the server used every granted byte)

How quickly a real client runs into the stall depends on its connection window and how it replenishes it; each lost
reservation is at most the data of one write, and the losses accumulate for the life of the connection.


AI disclosure (ASF Generative Tooling Guidance): found
with a TLA+ model and reproduced on a Tomcat build. Claude Code (Claude Opus 5.5) wrote the model, the reproducer,
the change, the tests and this text; the change was reviewed by Claude Fable 5.2 and Gemini 4 Neon.

🤖 Generated with Claude Code

StreamOutputBuffer.flush() reserves HTTP/2 connection flow control
window and then calls writeBody(), which sends nothing if the stream can
no longer write. If the stream was reset between the two, the reserved
window was lost and the connection could stall waiting for a window
update the client had no reason to send.

writeBody() now reports whether it wrote, and flush() releases the
reservation when it did not. The sendfile path uses the same release
method.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vzhn
vzhn marked this pull request as ready for review November 29, 2026 08:26
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.

1 participant