Skip to content

Restore retryable classification for Http2Client response body failures - #3694

Merged
velo merged 2 commits into
OpenFeign:masterfrom
Inf1mum:fix/2689-java11-retry
Nov 24, 2026
Merged

velo merged 2 commits into
OpenFeign:masterfrom
Inf1mum:fix/2689-java11-retry

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2689

Problem

Since #2420, Http2Client reads the response body lazily, so a malformed or truncated body (e.g. WireMock's Fault.MALFORMED_RESPONSE_CHUNK) fails only after client.execute() has returned. Feign classifies it as errorReading instead of the retryable errorExecuting, and with a Response return type the raw IOException escapes the invocation, so retries never fire.

Fix

Http2Client(HttpClient client, boolean bufferResponseHead) opts into reading up to 8KB of the raw body inside execute(), so a malformed chunk or truncation there surfaces as an IOException and is classified as retryable. The existing constructors delegate with false: by default nothing changes and the body is still streamed lazily, which streaming consumers rely on. Nothing is added to Request.Options or Feign.Builder.

Larger bodies keep streaming, decompression failures still surface in the decoder, and the async (AsyncFeign) path is unchanged. execute() blocks until 8KB or EOF, hence opt-in.

Feign.builder().client(new Http2Client(httpClient, true)) // ...

Testing

New tests in Http2ClientTest: responseIsStreamedLazilyByDefault and malformedResponseChunkIsReadLazilyByDefault pin the default lazy behaviour; bufferedResponseHeadSurfacesMalformedChunkAsRetryableException and bufferedResponseHeadKeepsStreamingBodiesLargerThanTheBuffer cover the opt-in client.

mvn -pl java11 -am package -Dtoolchain.skip=true -Dlicense.skip=true -Dtest='Http2Client*' -Dsurefire.failIfNoSpecifiedTests=false passes (112 tests in the 6 Http2Client* classes), as does git-code-format:validate-code-format. The decompression probe from the first revision was not re-run on this revision.

Since OpenFeign#2420, Http2Client delivers response bodies via
BodyHandlers.ofInputStream(): send() returns as soon as the headers
arrive and the body is read lazily. A malformed or truncated body - eg
WireMock's Fault.MALFORMED_RESPONSE_CHUNK - then fails during decoding,
after client.execute() returned successfully: feign classifies such
failures as errorReading (a plain FeignException) instead of the
retryable errorExecuting, and with a Response return type the raw
IOException escapes the invocation entirely, so retries never fire.

Read the head (up to 8KB) of the raw, pre-decompression response body
during execute(), mirroring the bounded eager read feign core applies
when it buffers response bodies. Transport failures on small responses
now surface from execute() as IOExceptions, restoring the retryable
classification responses had before the switch to ofInputStream().
Decompression failures keep surfacing in the decoder, and bodies
larger than the buffer keep streaming: the buffered head is
re-attached in front of the remaining stream, preserving the memory
behavior that motivated OpenFeign#2420. The async (AsyncFeign) path is
unchanged and keeps streaming lazily.

Fixes OpenFeign#2689

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>

@velo velo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into this, and for the very clear write-up. I agree with the diagnosis: since the switch to BodyHandlers.ofInputStream(), a broken body only fails after execute() has returned, so it gets classified as errorReading (or escapes entirely with a Response return type) instead of the retryable errorExecuting. Getting that back is a valuable fix.

My one concern is backwards compatibility for streaming consumers. With this change execute() blocks until it has 8KB or EOF, where today it returns as soon as the headers arrive. For SSE, long-polling, chunked streams, or anything else that trickles out less than 8KB at a time, that's a real behaviour change: the call can sit there, or even hit the read timeout, before the caller ever sees the Response. Some existing users rely on getting the stream back right away.

Could we make the eager head read opt-in, with the default staying exactly as it is today?

As a suggestion (very open to other ideas if you see a better fit): the closest precedent in feign for a client-level transport knob is DefaultClient's boolean disableRequestBuffering. DefaultClient(SSLSocketFactory, HostnameVerifier) keeps the default, and the DefaultClient(SSLSocketFactory, HostnameVerifier, boolean disableRequestBuffering) overload lets you opt in. Following that pattern here, e.g. Http2Client(HttpClient client, boolean bufferResponseHead) (name up to you), with the existing constructors delegating with false, would keep this scoped to Http2Client without touching Request.Options or Feign.Builder. Those are core-wide and would need every other client to honour the flag.

I also left a small inline suggestion: a TODO next to MAX_RESPONSE_HEAD_SIZE.

* Maximum number of response body bytes read eagerly, mirroring the size bound feign core uses
* when it buffers response bodies ({@code InvocationContext.MAX_RESPONSE_BUFFER_SIZE}).
*/
private static final int MAX_RESPONSE_HEAD_SIZE = 8192;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we leave a TODO here? This size (and InvocationContext.MAX_RESPONSE_BUFFER_SIZE in core) should eventually be configurable, probably as 14.x work.

Suggested change
private static final int MAX_RESPONSE_HEAD_SIZE = 8192;
// TODO: make this configurable, together with InvocationContext.MAX_RESPONSE_BUFFER_SIZE in
// core, which shares the same hardcoded 8KB bound (likely 14.x work).
private static final int MAX_RESPONSE_HEAD_SIZE = 8192;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 2028352, with your wording. I put it above the Javadoc of the constant rather than between the Javadoc and the field, so the Javadoc stays attached to the declaration.

Reading the head of the response body during execute() changes the
behaviour streaming consumers rely on: execute() used to return as soon
as the headers arrive, and now blocked until 8KB or EOF, which does not
suit SSE, long polling or chunked streams that trickle out.

Follow the DefaultClient#disableRequestBuffering precedent and make it
opt-in: Http2Client(HttpClient, boolean bufferResponseHead) enables it,
the existing constructors delegate with false and keep today's
behaviour - execute() returns with the headers and the body is streamed
lazily. The buffered body is handed to the existing
toFeignResponse(Request, HttpResponse) through a view of the response,
so that overrides of that method keep being honoured.

Tests: the default client returns a Response while the server still
holds a later chunk, and a malformed chunk keeps its lazy classification
(raw IOException when the caller reads the body, non-retryable
FeignException for a decoded return type). The opt-in client surfaces
the malformed chunk from execute() as a RetryableException and keeps
streaming bodies larger than the buffer.

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>
@Inf1mum

Inf1mum commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @velo — agreed, that is a behaviour change I don't want to make for streaming consumers. Done in 2028352, following the DefaultClient / disableRequestBuffering precedent:

  • Http2Client(HttpClient client, boolean bufferResponseHead) enables the head read. The existing constructors delegate with false, so by default nothing changes: execute() returns as soon as the headers arrive and the body is streamed lazily. Request.Options and Feign.Builder are untouched.
  • The TODO is in, above the Javadoc of MAX_RESPONSE_HEAD_SIZE.
  • One thing I changed while there: the buffered body is now handed to the existing toFeignResponse(Request, HttpResponse) through a thin view of the response, instead of through the extra overload the first revision had. So overrides of toFeignResponse keep being honoured in both modes, and the only addition to the API of the class is the constructor.

Tests, all in Http2ClientTest:

  • Default client, responseIsStreamedLazilyByDefault: a raw socket server sends a first chunk behind the headers and holds the rest back until the test has the Response in hand. execute() has to return while the second chunk is still pending; the stream then delivers it.
  • Default client, malformedResponseChunkIsReadLazilyByDefault: pins today's classification. execute() returns normally, the malformed chunk is a raw IOException when the caller reads the body of a Response and a non-retryable FeignException (errorReading) for a decoded type, and the retryer is never consulted.
  • Opt-in client, bufferedResponseHeadSurfacesMalformedChunkAsRetryableException (the regression test, re-pointed): RetryableException for both return types, 6 attempts over two invocations.
  • Opt-in client, bufferedResponseHeadKeepsStreamingBodiesLargerThanTheBuffer: a 100KB chunked body arrives intact, decoded and read from a Response.

Numbers: the Http2Client* classes on JDK 25 — 112 tests in 6 classes, all pass. With the default temporarily flipped to eager (this(client, true)), the two default-client tests fail: responseIsStreamedLazilyByDefault with RetryableException: response body read timed out after the 2s read timeout (the call sits on the body before the caller ever sees the Response), the other with a RetryableException from execute(); the remaining 39 pass. With master's Http2Client swapped in, both default-client tests pass. The format validation passes for the module.

The description is updated accordingly (opt-in, why, how to enable it).

@velo
velo merged commit 8f6351d into OpenFeign:master Nov 24, 2026
3 checks passed
velo pushed a commit that referenced this pull request Nov 27, 2026
* Hold the malformed chunk back until execute() returned

The stub of malformedResponseChunkIsReadLazilyByDefault (added in
#3694) wrote the malformed chunk right after draining the request, in
the same bytes the client parses to complete send(). Depending on
thread scheduling, send() itself could parse the chunk, feign then
classifies the IOException as a RetryableException and the retryer is
consulted - the CI failure showed `attempts` expected 0 but was 1.

The stub now sends only the status line and headers, and writes the
malformed chunk only after the wrapped client awaited a
`CyclicBarrier` released right after every `execute()` returned.
The chunk can then no longer be parsed inside `send()`: the failure
always surfaces on a later read of the body, exactly what the test
asserts, and it is still written within 10s of execute() returning.

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>
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.

Retry issue after migration to 13.5

2 participants