Repository navigation
Drop held input of a body cut off by the close - #2386
Merged
hyperxpro merged 3 commits intoNov 14, 2026
Merged
Conversation
When the connection closed while a suspended response held compressed input back, Http1ContentDecompressor decompressed all of it at once and handed it on. For a body that the close cut off, that work is wasted: the response cannot complete, and the request fails or is retried right after. The input held back is now dropped unless the end of the body has arrived, either held with the input or queued behind it. A body that has ended is handed on at the close as before: a held last chunk, the end of a body delimited by the connection, and a last chunk that failed to parse. A dropped body is not decompressed first, so it no longer fails on a corrupt or oversized remainder. The request is handled like any other whose connection closed before the end of its body: it is retried or fails with a RemotelyClosedException, and the IOExceptionFilters see a ChannelClosedException. Whether an orderly close by the server (a TCP FIN) is noticed while reads are suspended depends on the transport: native transports such as epoll notice it, NIO only once the input held back has been used up. A close on the client's side, for example when the client is closed, reaches the decompressor on every transport. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
Author
|
I also have a local prototype for what is still open here: a body that has ended, held back when the server closes the connection in an orderly way (a TCP FIN). While compressed input is held back, it turns on half-closure, so that close only shuts the input down. The rest of the body then still waits for demand, and the connection is not reused. Tests cover epoll and io_uring, over plaintext and TLS; kqueue is untested. A reset, a close on the client's side and HTTP/2 are not covered. Would you like it as a separate PR after this one? |
hyperxpro
requested changes
Nov 9, 2026
A last chunk that HttpObjectDecoder could not parse counted as the end of a paused body, so at the close the input held back was decompressed and handed on at once. A server that sends half a body, then a bad chunk size line and closes got the old behaviour back: the rest of the body reached the suspended handler, or the request failed with the size limit instead of the parser's error. Only a successful end now lets the input held back be handed on at the close. For a failed one it is dropped, and the failed end is still handed on, so that the request fails with the parser's error. The test of a body cut off by the close used zeros, which every coding but snappy compresses into a single fragment, so no input was queued behind the input held back. It now uses a payload that barely compresses, and checks that input is queued. Add a test that a body cut off by the close is retried with the default retries. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Javadoc of ResponseBodyControl.suspend() said that a body cut off by the close fails, but the request is retried first with the default retries, and a body delimited by the connection always ends at the close, so its held input is decompressed and fails the request if the compressed stream is incomplete. It also said that native transports notice a close by the server while reads are suspended, but they read everything that is left, so a body without a content coding that arrived before the close is delivered in full to a suspended handler. Describe both, and that a last chunk that could not be parsed fails the request with that error. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 <[email protected]>
hyperxpro
pushed a commit
that referenced
this pull request
Nov 21, 2026
Motivation: Since #2386, when the connection closes while a suspended HTTP/1.1 response holds compressed input back, a body that the close cut off is dropped, but the rest of a body that has ended is still decompressed and handed on at once, bounded only by `maxDecompressedResponseSize`. Netty tears the pipeline down with the close, so nothing can wait for demand. epoll and io_uring read the close even while reads are suspended, so this happens whenever the server closes right after the body, for example with `Connection: close` or a body delimited by the connection. With 16 MiB of zeros as gzip and a handler that suspends after every part and never resumes, `main` delivers 257 parts at once where NIO delivers 1. This is the follow-up promised on #2362 for output left over when the connection closes, the case #2386 left open. Modification: - While input is held back, `Http1ContentDecompressor` turns `ALLOW_HALF_CLOSURE` on for the channel if it was off. The server's FIN then reaches the pipeline as a `ChannelInputShutdownEvent` instead of a close. - On that event, a body that has ended successfully, including one delimited by the connection, keeps waiting for demand. `NettyResponseFuture.setKeepAlive(false)` makes sure the connection is closed once the response is done instead of being offered to the pool. - A body that the close cut off, or whose last chunk could not be parsed, does not wait. The channel is closed at once and `channelInactive` drops the input held back, as before. - The wait applies only while the request is still attached to the connection (`future.channel() == ctx.channel()`). After a reset, `AsyncHttpClientHandler.exceptionCaught` has already detached it and expects the close, so the channel is closed at once. - Half-closure is turned off again once nothing is held back, at the latest before the end of the body is handed on, and when the handler is removed. If the application turned it on itself, it is left on. - The Javadoc of `ResponseBodyControl.suspend()` and of `Http1ContentDecompressor` describe the new behaviour. - Overriding `userEventTriggered` drops Netty's internal `@ChannelHandlerMask.Skip` marker, so `pom.xml` gets the same scoped Revapi ignore as `Http2Handler::userEventTriggered`. Result: After an orderly close by the server, the rest of a suspended compressed body that has ended is delivered one part per resume on epoll and io_uring, over HTTP and HTTPS, and the connection is closed when the response is done. A connection is never offered to the pool with half-closure on or after the server shut its input down. Not covered: a close on the client's side, a reset or any other close still reaches `channelInactive`, where the rest of a body that has ended is decompressed and handed on at once. kqueue should behave like epoll but was not run. Bodies without a content coding and HTTP/2 are unchanged. The new tests in `Http1ContentDecompressorSuspensionTest` and `Http1DecompressionSuspensionTest` fail on `main`. `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11: 2,168 tests, 0 failures, Revapi passed. Claude Code on behalf of Matthias Kurz. --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
30 of 31 tasks
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.
Summary
Problem
Http1ContentDecompressorholds compressed input back while a response is suspended. When the connection closes, Netty tears the pipeline down in the same task, so onmaineverything held back is decompressed and handed on at once, bounded only bymaxDecompressedResponseSize.For a body that the close cut off, a
Content-Lengthor chunked body that never reached its end, that is wasted work: the response cannot complete, and AHC fails the request (RemotelyClosedException) or retries it right after. A suspended handler still receives the whole remainder first.Whether an orderly close by the server (a TCP FIN) is noticed while the response is suspended depends on the transport. A handler that suspends after its first part and never resumes, 16 MiB of zeros as gzip, and a server that closes the connection:
mainRemotelyClosedExceptionRemotelyClosedExceptionNIO honors auto-read, so it reads nothing, the close included, until a resume. epoll keeps reading after
EPOLLRDHUPregardless of auto-read (EpollRecvByteAllocatorHandle.continueReading), io_uring behaves the same here, and so does kqueue (KQueueRecvByteAllocatorHandle.continueReading), so they read everything the server sent before its close. AHC selects epoll on Linux whenever the native library is on the classpath. A close on the client's side, for example when the client is closed, reaches the decompressor on every transport.Change
channelInactive, the input held back is decompressed only if the body has ended successfully: aLastHttpContentwith a successful decoder result, held with the input or queued behind it. That covers the end of a body delimited by the connection, whichHttpObjectDecoderhands on just before the close. Otherwise the input is dropped with the rest of the response state.HttpHandlerfails the request with the parser's error, without a retry.DecompressionException, which is never retried. A body that the close cut off is handled like any other whose connection closed before the end of its body: it is retried (maxRequestRetry, or anIOExceptionFilterthat replays it) or fails with aRemotelyClosedException, and theIOExceptionFilters see aChannelClosedException. As for a body without a content coding, the method does not decide whether it is retried, so a POST can be retried too.ResponseBodyControl.suspend()and of the class say so.suspend()also says that after an orderly close by the server, epoll, io_uring and kqueue read what is left even while reads are suspended, so a body without a content coding that arrived before the close is delivered in full, and that NIO reads nothing until a resume.Still open: a body that has ended, including one delimited by the connection, is still decompressed and handed on at once when the connection closes while its input is held back, on native transports, bounded by
maxDecompressedResponseSizeas the Javadoc already said. Keeping it to demand needs the input held back to outlive the close. Either the channel allows half-closure, so that the server's close only shuts its input down and the pipeline stays until the body has been delivered and the channel is closed, or the input held back is moved out of the pipeline before it is torn down. Either is a larger change, left for a separate PR. HTTP/2 is separate.AI disclosure
Claude Code on behalf of Matthias Kurz. The commits include
Co-Authored-ByperAGENTS.md.Test plan
main, pass with this PR. The epoll tests were run againstmain's code, where all four cases fail with the size limit'sDecompressionException. The unit tests use a test hook thatmainlacks, so they were checked withmain's close handling put back:Http1ContentDecompressorSuspensionTest.inputHeldBackOfABodyThatTheCloseCutsOffIsDropped(gzip, deflate, br, zstd, snappy): a body without its end, suspended after its first part, with a limit that decompressing the rest would exceed. 512 KiB of random bytes, so that input is also queued behind the input held back for every coding, which the test checks. At the close nothing more is handed on, no error, every input buffer released, andchannelInactivepassed on.Http1ContentDecompressorSuspensionTest.inputHeldBackOfABodyWhoseLastChunkFailedIsDroppedAtTheClose(same five codings and payload): a failed last chunk queued behind the input held back. At the close nothing more is handed on, and the end is handed on with the parser's error.Http1DecompressionSuspensionTest.aSuspendedBodyThatTheConnectionCutsOffFailsWithoutTheRestOfIt(Linux with epoll; aContent-Lengthand a chunked body, half of it sent, then closed; no retries): the handler gets 1 part, the request fails withRemotelyClosedException, and theIOExceptionFiltersees oneChannelClosedException.Http1DecompressionSuspensionTest.aSuspendedBodyThatTheConnectionCutsOffIsRetried(epoll, a GET, default retries): the server sees 1 +maxRequestRetryrequests, each delivers its first part only, and the request then fails withRemotelyClosedException.Http1DecompressionSuspensionTest.aSuspendedBodyWhoseLastChunkCannotBeParsedFailsWithTheParsersError(epoll; half a chunked body, thenzzas the chunk size line, then the close; 1 MiB limit, default retries): the handler gets 1 part, the request fails with theNumberFormatExceptionfromHttpObjectDecoder, and the server sees 1 request.mainand with this PR:Http1ContentDecompressorSuspensionTest.inputHeldBackOfABodyThatHasEndedIsHandedOnAtTheClose(gzip, zstd; the end of a body delimited by the connection): the whole body is handed on at the close, then the end.channelClosureHandsOnHeldInputBeforeChannelInactive,aTruncatedBodyHeldWhenTheConnectionClosesFailsandaHeldDirectInputIsReleasedWhenTheResponseEndsEarly, for a held last chunk.inputHeldBackOfABodyThatTheCloseCutsOffIsDroppedfails for all five codings, and so do the epoll tests;./mvnw -B -ntp -Dgpg.skip=true clean verifyon JDK 11: 2,009 tests, 0 failures, 0 errors, 11 skipped; Revapi passed. No test-skipping flags were used.Http1ContentDecompressor*Test,Http2ContentDecompressorTest,Http1Decompression*TestandResponseBodyControlTestclasses compiled on JDK 11 and run on JDK 17, 21 and 25 (Maven Surefire's-Djvm): 140 tests each, 0 failures.