Skip to content

Fix connection pool leak when response is not modified - #4260

Open
arimu1 wants to merge 1 commit into
spring-cloud:mainfrom
arimu1:fix/4259-not-modified-connection-leak
Open

Fix connection pool leak when response is not modified#4260
arimu1 wants to merge 1 commit into
spring-cloud:mainfrom
arimu1:fix/4259-not-modified-connection-leak

Conversation

@arimu1

@arimu1 arimu1 commented Aug 8, 2026

Copy link
Copy Markdown

Fixes #4259

Description

When a proxied GET/HEAD response is short-circuited as HTTP 304 Not Modified in AbstractGatewayServerResponse#writeTo, writeToInternal is never invoked. That path is where RestClientProxyExchange / ClientHttpRequestFactoryProxyExchange close the upstream ClientHttpResponse and return the connection to the pool.

As a result, with Apache HttpComponents Client 5, leased connections are not released after conditional requests that evaluate to not modified.

Change

On the not-modified short-circuit, close the ClientHttpResponse stored in MvcUtils.CLIENT_RESPONSE_ATTR (same attribute the reporter’s workaround interceptor closed), then clear the attribute.

No change to the 304 short-circuit behavior itself—only resource cleanup when the body write is skipped.

Tests

  • RestClientProxyExchangeTests#exchangeWhenNotModifiedThenClosesClientResponse (If-None-Match + matching ETag → 304 and ClientHttpResponse.close())
  • Existing streaming disconnect test still passes
  • ./mvnw -pl spring-cloud-gateway-server-webmvc -am test -Dtest=RestClientProxyExchangeTests (Temurin 21): 2/2
  • checkstyle + spring-javaformat validate clean on the module

Notes

When checkNotModified short-circuits writeTo for GET/HEAD, writeToInternal
never runs, so RestClientProxyExchange does not close the upstream
ClientHttpResponse and Apache HttpClient connections stay leased.

Close the ClientHttpResponse stored in CLIENT_RESPONSE_ATTR on the
not-modified path so the connection returns to the pool.

Fixes spring-cloudgh-4259

Signed-off-by: arimu1 <19286898+arimu1@users.noreply.github.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.

Connection pool leaks when response not modified

2 participants