Repository navigation
Do not resend a consumed multipart stream part - #2398
Merged
hyperxpro merged 2 commits intoNov 22, 2026
Merged
Conversation
A retry, a redirect that keeps the body, or a replay by a filter builds a new multipart body over the same InputStreamPart streams and reads them from wherever the first attempt left them. With the default configuration, AHC retries a request whose connection the server closed without a response. If the part had no length and its stream returns end of stream once read, as a ByteArrayInputStream does, the retry sent an empty part and the server's answer completed the request. With a known length, the retry declared the full length but sent only the part headers, so it waited for a timeout. With a FileInputStream, it failed with a ClosedChannelException. NettyResponseFuture now records, by identity, the stream of each part that a write of the request has started on. A later write that finds a recorded stream resets it if it supports mark/reset, as NettyInputStreamBody does for an InputStream body, and otherwise fails the request with an IOException before the body is written. The streams of parts that a filter supplies for a replay are sent as they are. NettyBodyBody.write does not declare IOException, so NettyRequestSender runs the check before the HTTP/1.1 and HTTP/2 writes. The redirect check now rejects an InputStreamPart only if a write has started on its stream and the stream does not support mark/reset. A redirect before 100 Continue, when nothing was sent, keeps the part again, as it did before AsyncHttpClient#2316, and a stream that supports mark/reset is replayed. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hyperxpro
requested changes
Nov 21, 2026
A retry, redirect or replay reset the stream of an InputStreamPart that an earlier write of the request had started on, if the stream supported mark/reset. That is not safe: - The earlier write may still be reading the stream, when a 307 arrives during the upload. - The part closes its stream once written, so a BufferedInputStream over a file passed the redirect check and then failed on the new connection. - The mark may be before where the earlier write started, and the part then writes more than its declared length. Such a stream is no longer reset. NettyRequestSender.sendNextRequest fails the request before connecting when one of the InputStreamParts it sends was consumed, so nothing of it is sent, over HTTP/1.1 or HTTP/2. Parts that it does not send, for a CONNECT or behind a body with higher precedence, are not checked. A connection kept for the request, as after a 401 on a keep-alive connection, is closed. The redirect check rejects a consumed stream part again, whatever its mark/reset support, and still keeps a part whose write had not started. The streams of parts that a filter supplies for a replay are still sent. Claude Code on behalf of Matthias Kurz Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hyperxpro
approved these changes
Nov 22, 2026
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
ResponseFilterorIOExceptionFilterwrites a new multipart body over the sameInputStreamPartstreams, and reads them from wherever the earlier attempt left them. AnInputStreambody already fails cleanly in that case (Failed stream request hangs until timeout #1973); multipart had no such check.InputStreamPart:ByteArrayInputStream, or a wrapper that ignoresclose()): the retry sends an empty part, the server answers, and the request succeeds.Content-Lengthbut sends only the part headers, so it waits for a timeout.FileInputStream: the retry fails with aClosedChannelException.InputStreamPart, even after a redirect before100 Continue, when nothing was sent yet.Change
NettyResponseFuture: records, by identity, the stream of each part that a write of the request has started on (markStreamPartConsumed,isStreamPartConsumed). The set is only created for a request with stream parts.NettyRequestSender:InputStreamParts.sendNextRequest, which retries, redirects, authentication retries and replays go through, fails the request withIOException: Multipart InputStream body part 'file' already consumed and cannot be sent againif one of itsInputStreamParts has a recorded stream. It does so before connecting, so nothing of that request is sent.CONNECTsends none, and neither does a request with a body of higher precedence, whichsetBodyPartsleaves in place, such as a string body that a filter set for its replay.401or407on a keep-alive connection, is closed, so it does not keep its slot undermaxConnections.Redirect30xInterceptor: rejects anInputStreamPartonly if a write has started on its stream. A redirect before100 Continuekeeps the part again.InputStreambody (isStreamConsumed) is unchanged, and multipart does not use it.MultipartStreamPartReplayTest:401on a keep-alive connection: the authentication retry fails, and withmaxConnectionsat 1 the next request is sent;ResponseFilterreplaces the parts for its replay: a fresh stream part is sent, from its current position even if it supports mark/reset; a reused stream part next to a fresh one fails the replay, and the server receives no second request;ResponseFiltersets a string body for its replay and keeps the consumed parts: the string body is sent; it changes the method toCONNECTand keeps the parts: theCONNECTis sent.Http2MultipartStreamPartReplayTest: aResponseFilterreplay over HTTP/2 fails for a consumed stream part, and the server receives no second HEADERS frame; a fresh stream part from the filter is sent.RedirectBodyTest:100 Continue, without being read before the redirect;BufferedInputStreamover the file.NettyRequestSenderConsumedBodyRetryTest: aResponseFilterreplay with a consumed stream part fails promptly.Notes
InputStreambody, a stream counts as consumed once the body write starts, not once the stream is read. A retry after a write that failed before it reached the stream part therefore fails too.InputStreambody (Failed stream request hangs until timeout #1973), a consumed stream part is not reset even if it supports mark/reset. AResponseFilterorIOExceptionFiltercan replay such a request with new parts.NettyResponseFuture.markStreamPartConsumedandisStreamPartConsumedare new public methods.AI disclosure
Claude Code on behalf of Matthias Kurz. The commits include
Co-Authored-ByperAGENTS.md.Test plan
./mvnw -B -ntp -Dgpg.skip=true clean verifyon JDK 11, on this branch and on this branch merged withmainat 3d0a347: 2,222 and 2,228 tests, 0 failures, 0 errors, 22 skipped; Revapi passed. Docker was not running, so the 22 skipped include the Testcontainers proxy tests. No test-skipping flags were used.NettyInputStreamBodyTestandRedirectRefusalTest: 113 tests, 0 failures, on JDK 11, 17, 21 and 25.client/src/main, the retry tests, the authentication retry test, the tests that reuse a stream part over HTTP/1.1 and HTTP/2, the replay test inNettyRequestSenderConsumedBodyRetryTestand the redirect test before100 Continuefail: the retries and replays succeed or time out, and the redirect fails withcannot be replayed after redirect. The tests with only fresh stream parts, a string body or aCONNECTpass onmainas well, and so do the redirect tests that expect a refusal, asmainrefuses every stream part on redirect.ClosedChannelExceptioninstead of the refusal, theBufferedInputStreamredirect fails after connecting to the target withalready consumed and cannot be reset for a retry, and the retries, authentication retries and replays of a consumed stream that supports mark/reset succeed, including the one whose mark is before its start.MultipartStreamPartReplayTestandHttp2MultipartStreamPartReplayTest: 10 runs, 0 failures.