Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/cosmos/azure-cosmos-test/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Breaking Changes

#### Bugs Fixed
* Fixed Netty ByteBuf leak in `GatewayServerErrorInjector` fault injection delay paths under HTTP/2. - See [PR 48880](https://github.com/Azure/azure-sdk-for-java/pull/48880)

#### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.azure.cosmos.implementation.routing.PartitionKeyInternalHelper;
import io.netty.channel.ConnectTimeoutException;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.util.ReferenceCountUtil;
import reactor.core.publisher.Mono;

import java.net.URI;
Expand Down Expand Up @@ -174,11 +175,18 @@ public Mono<HttpResponse> injectGatewayErrors(
if (this.injectGatewayServerResponseDelayAfterProcessing(faultInjectionRequestArgs, delayToBeInjected)) {
if (delayToBeInjected.v.toMillis() >= effectiveResponseTimeout.toMillis()) {
return originalResponseMono
.doOnNext(GatewayServerErrorInjector::releaseResponseBody)
.delayElement(delayToBeInjected.v)
.then(Mono.error(new ReadTimeoutException()));
Comment thread
jeet1995 marked this conversation as resolved.
} else {
// In the happy path the response flows downstream after the delay
// and the caller drains the body normally. However, if the downstream
// cancels during delayElement (e.g., an outer timeout fires),
// delayElement silently drops the buffered HttpResponse. doOnDiscard
// catches that discard and releases the body ByteBuf to prevent leaks.
return originalResponseMono
.delayElement(delayToBeInjected.v);
.delayElement(delayToBeInjected.v)
.doOnDiscard(HttpResponse.class, GatewayServerErrorInjector::releaseResponseBody);
}
}

Expand Down Expand Up @@ -243,4 +251,17 @@ private GatewayFaultInjectionRequestArgs createFaultInjectionRequestArgs(
serviceRequest,
partitionKeyRangeIds);
}

/**
* Drains and releases the response body to prevent Netty ByteBuf leaks.
* Called when a fault-injected path discards the real HTTP response (e.g.,
* simulating a timeout after the response has already arrived).
*/
private static void releaseResponseBody(HttpResponse httpResponse) {
httpResponse.body()
.subscribe(
buf -> ReferenceCountUtil.safeRelease(buf),
ex -> { },
() -> { });
}
}
Loading