Repository navigation
Conversation
A suspended response that held compressed input back still had all of a body that had ended decompressed and handed on at once when the server closed the connection, on native transports such as epoll, which read the close even while reads are suspended. Only the channel's pipeline held that input, and it is torn down with the close. While input is held back, Http1ContentDecompressor now turns on half-closure for the channel, so that an orderly close by the server only shuts the input down. The end of the body then decides, as it does on a close: - the rest of a body that has ended, including one delimited by the connection, still waits for demand, and the response no longer keeps the connection alive, so it is closed once the response is done; - a body that the close cut off, or whose last chunk could not be parsed, does not wait: the channel is closed at once, which drops what is held back of it, and the request is retried or fails as before. Half-closure is turned off again once nothing is held back, at the latest before the end of the body is handed on, so a connection is not offered for reuse with it on. If it was on already, it is left on, and a close by the server while input is held back is handled the same way. NIO reads only when asked to, so in the tested cases it saw the close only once the input held back had been used up and half-closure was off again. TLS can still ask for a read while reads are suspended, for example to complete a record. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Since #2386, when the connection closes while input is held 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. On native transports this happens whenever the server closes the connection right after the body, for example withConnection: closeor a body delimited by the connection, because they read the close even while reads are suspended.A handler that suspends after every part and does not resume, 16 MiB of zeros as gzip, and a server that closes the connection after sending it:
mainand this PRmainRemotelyClosedExceptionRemotelyClosedExceptionThe same over TLS (258 parts).
Change
Http1ContentDecompressorturnsALLOW_HALF_CLOSUREon for the channel, if it was off. The server's FIN then reaches the pipeline as aChannelInputShutdownEventinstead of a close.ByteToMessageDecoder, and soHttpObjectDecoder, already handles that event like a close and hands on the end of a body delimited by the connection.NettyResponseFuture.setKeepAlive(false)), so the connection is closed once the response is done instead of being offered to the pool.channelInactivedrops the input held back as before. A failed last chunk still fails the request with the parser's error; a cut-off body is retried or fails withRemotelyClosedException.main, such a connection was offered to the pool after the server had shut its input down.ResponseBodyControl.suspend()and of the class say so.userEventTriggereddrops Netty's internal@ChannelHandlerMask.Skipmarker. The same scopedjava.annotation.removedignore as forHttp2Handler::userEventTriggered.NIO reads only when asked to, so in the tested cases it saw the close only once the input held back had been used up and half-closure was off again. TLS can still ask for a read while reads are suspended, for example to complete a record; the tests have not seen that read the close. Half-closure is turned on there too while input is held back, and the NIO tests check that nothing else changes.
Not covered:
AsyncHttpClient.close()), a reset or any other close still reacheschannelInactive, where the rest of a body that has ended can still be decompressed and handed on at once, as before.AI disclosure
Claude Code on behalf of Matthias Kurz. The commit includes
Co-Authored-ByperAGENTS.md.Test plan
main, pass with this PR:Http1ContentDecompressorSuspensionTest(EmbeddedChannel with aNioSocketChannel's config, the event fired by hand):aCloseByTheServerOnlyShutsTheInputDownWhileInputIsHeldBack(gzip, deflate, br, zstd, snappy): half-closure on while input is held back; after the event nothing more is handed on, the channel stays open, keep-alive is off; the rest arrives one part per resume, and half-closure is off at the end.aCloseByTheServerFailsABodyThatItCutsOffAtOnceandaCloseByTheServerFailsABodyWhoseLastChunkFailedAtOnce(five codings each): the channel is closed at once, nothing held back is handed on, a failed end is handed on with the parser's error, every input buffer is released.halfClosureIsOffWhenTheEndOfTheBodyIsHandedOn(five codings),removalTurnsHalfClosureOff,discardingABodyHeldBackTurnsHalfClosureOff,halfClosureThatWasOnAlreadyIsLeftOn(ended and cut off).Http1DecompressionSuspensionTest(Linux):aSuspendedBodyThatHasEndedWaitsForDemandAfterTheServerCloses(epoll, io_uring; http, https; aContent-Lengthbody then a close, and a body delimited by the close): the input is shut down, 1 part, the response is not done; then at most one part per resume, the whole body, no offer to the pool, the connection closed.aSuspendedBodyThatTheServerCutsOffOrBreaksFailsAtOnce(epoll, io_uring; http, https; aContent-Lengthand a chunked body cut in half, and a chunked body whose next chunk size line iszz; 1 MiB limit, no retries): the input is shut down, the request fails withRemotelyClosedExceptionor the parser'sNumberFormatExceptionrather than the size limit, 1 part, the connection closed.theRequestTimeoutAfterTheServerClosesFailsTheResponse(epoll, io_uring; http, https): a 2 s request timeout after the input was shut down fails withTimeoutException, 1 part, the connection closed.cancellationAfterTheServerClosesClosesTheConnection(epoll, io_uring; http, https).aConnectionWithHalfClosureOnAlreadyIsNotReusedAfterTheServerCloses(epoll, io_uring): onmainthe connection is offered to the pool.aConnectionIsReusedWithHalfClosureOffAfterASuspendedBody(NIO, epoll, io_uring; keep-alive, no close): half-closure on while input is held back, off when the connection is offered to the pool, and the next request reuses the connection.onNioASuspendedBodyThatHasEndedWaitsForDemandAfterTheServerCloses(http, https; both closing bodies): after the server has closed the connection, 1 part, the response is not done, half-closure on; then the whole body on demand.The last two fail on
mainonly on the check that half-closure is on; their other checks pass there too.mainand with this PR: Drop held input of a body cut off by the close #2386'saSuspendedBodyThatTheConnectionCutsOffIsRetriedandaSuspendedBodyWhoseLastChunkCannotBeParsedFailsWithTheParsersError(epoll), which now go through the input shutdown and the close it triggers./zeros-bad-chunknow writes its raw chunk size line throughHttpServerCodec's context, so that it also works over TLS.main's decompressor: 61 tests fail../mvnw -B -ntp -Dgpg.skip=true clean verifyon JDK 11: 2,159 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): 203 tests each, 0 failures.Http1DecompressionSuspensionTestfive more times in a row: 51 tests each, 0 failures.