Repository navigation
Restore retryable classification for Http2Client response body failures - #3694
Conversation
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
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
| 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; |
There was a problem hiding this comment.
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>
|
Thanks @velo — agreed, that is a behaviour change I don't want to make for streaming consumers. Done in 2028352, following the
Tests, all in
Numbers: the The description is updated accordingly (opt-in, why, how to enable it). |
* 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>
Fixes #2689
Problem
Since #2420,
Http2Clientreads the response body lazily, so a malformed or truncated body (e.g. WireMock'sFault.MALFORMED_RESPONSE_CHUNK) fails only afterclient.execute()has returned. Feign classifies it aserrorReadinginstead of the retryableerrorExecuting, and with aResponsereturn type the rawIOExceptionescapes the invocation, so retries never fire.Fix
Http2Client(HttpClient client, boolean bufferResponseHead)opts into reading up to 8KB of the raw body insideexecute(), so a malformed chunk or truncation there surfaces as anIOExceptionand is classified as retryable. The existing constructors delegate withfalse: by default nothing changes and the body is still streamed lazily, which streaming consumers rely on. Nothing is added toRequest.OptionsorFeign.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.Testing
New tests in
Http2ClientTest:responseIsStreamedLazilyByDefaultandmalformedResponseChunkIsReadLazilyByDefaultpin the default lazy behaviour;bufferedResponseHeadSurfacesMalformedChunkAsRetryableExceptionandbufferedResponseHeadKeepsStreamingBodiesLargerThanTheBuffercover the opt-in client.mvn -pl java11 -am package -Dtoolchain.skip=true -Dlicense.skip=true -Dtest='Http2Client*' -Dsurefire.failIfNoSpecifiedTests=falsepasses (112 tests in the 6Http2Client*classes), as doesgit-code-format:validate-code-format. The decompression probe from the first revision was not re-run on this revision.