Skip to content

Fix HTTP connection leak when using feign.Response with chunked Transfer-Encoding#1346

Open
ban-xiu wants to merge 1 commit intospring-cloud:mainfrom
ban-xiu:fix-issue-1198
Open

Fix HTTP connection leak when using feign.Response with chunked Transfer-Encoding#1346
ban-xiu wants to merge 1 commit intospring-cloud:mainfrom
ban-xiu:fix-issue-1198

Conversation

@ban-xiu
Copy link

@ban-xiu ban-xiu commented Mar 22, 2026

Summary

Close #1198. When a @FeignClient method declares feign.Response as its return type and the server responds with Transfer-Encoding: chunked, the underlying HTTP connection is never released back to the connection pool, causing a connection leak.

Root Cause

Feign-core's InvocationContext.proceed() has a special path for feign.Response return type — it bypasses the entire decoder chain and calls disconnectResponseBodyIfNeeded(). This method only buffers the response body when body.length() != null && body.length() <= 8192.

For chunked responses, body.length() returns null (content length is unknown), so the buffering is skipped and the raw Response with a live InputStream tied to the pooled HTTP connection is returned directly to the caller. If the caller does not explicitly close the Response, the connection is permanently leaked.

Fix

Register a default ResponseInterceptor bean in FeignClientsConfiguration that intercepts the response before it reaches feign-core's disconnectResponseBodyIfNeeded().
When the return type is feign.Response and the body has unknown length (chunked), the interceptor:

  1. Buffers the body into a byte[]
  2. Closes the original response (releasing the HTTP connection)
  3. Rebuilds the response with the in-memory body

Signed-off-by: lanbinshen <lanbinshen@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTTP connection leak when using feign.Response and apache HTTP client Connection pool

2 participants