Repository navigation
Conversation
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
marked this pull request as ready for review
November 29, 2026 08:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StreamOutputBuffer.flush()reserves connection flow control window withHttp2UpgradeHandler.reserveWindowSize()and then calls
writeBody().writeBody()(sync and async) checksstream.canWrite()and sends nothing if thestream 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 += nChange
writeBody()(Http2UpgradeHandlerandHttp2AsyncUpgradeHandler) now returns whether it wrote the frame.StreamOutputBuffer.flush(), which made the reservation, releases it whenwriteBody()did not write, so thereservation and its release stay in one place.
Http2UpgradeHandler.releaseConnectionAllocation(int)releases unused connection window throughincrementWindowSize(), so streams waiting in the backlog are released as for a WINDOW_UPDATE. If the release wouldtake 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 usesit 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 undertheir 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 andreplaceStream()returns it when the stream is closed, as for any allocation a reset stream did not use.Tests
TestHttp2ConnectionWindowResetmakes the interleaving deterministic: a testHttp2Protocolcreates handlers whosereserveWindowSize()callsstream.receiveReset(CANCEL)(what processing the client's RST_STREAM does) as soon asit 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.
testResetAfterConnectionWindowReservedexpected:<57343> but was:<56319>(1,024 bytes lost)testResetAfterConnectionWindowAllocatedFromBacklogexpected:<1024> but was:<0>(the whole update lost)All
org.apache.coyote.http2tests 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 thelost 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 begranted 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:
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