Skip to content

Fix connection leak in CircuitBreaker fallback scenario with chunked responses#4104

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

Fix connection leak in CircuitBreaker fallback scenario with chunked responses#4104
ban-xiu wants to merge 1 commit intospring-cloud:mainfrom
ban-xiu:fix-issue-3978

Conversation

@ban-xiu
Copy link

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


Problem

Close #3978. When Spring Cloud Gateway uses CircuitBreaker with fallback, if the backend returns a response with chunked transfer encoding, the connection to the backend is not properly released, causing a connection pool leak. Eventually, the connection pool becomes exhausted, and subsequent requests fail with 504 GATEWAY_TIMEOUT due to PoolAcquireTimeout.

Root Cause

The issue is in ServerWebExchangeUtils.reset() method. When CircuitBreaker triggers fallback, it calls reset(exchange) to prepare the exchange for the fallback request. However, the current implementation only:

  1. Removes added response headers
  2. Clears the alreadyRouted flag

It does not release the client connection stored in CLIENT_RESPONSE_CONN_ATTR, which was established by NettyRoutingFilter to the backend.

Connection leak flow:

Request → NettyRoutingFilter creates connection → stored in CLIENT_RESPONSE_CONN_ATTR
    ↓
Backend returns 500 + chunked response
    ↓
CircuitBreaker triggers fallback
    ↓
reset(exchange) is called, but connection is NOT released
    ↓
Fallback request uses a new connection
    ↓
Original connection leaks forever → pool exhaustion

Solution

Enhance ServerWebExchangeUtils.reset() to properly dispose the client connection


Signed-off-by: lanbinshen <lanbinshen@xiaomi.com>
@ban-xiu
Copy link
Author

ban-xiu commented Mar 21, 2026

@ryanjbaxter Thank you for your hard work reviewing this PR. I really appreciate it.

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 leak - CircuitBreak Filter + Chunked 500 Response

2 participants