Skip to content

Fixed the issue where AWS CRT HTTP client was eagerly buffering data … #6260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS CRT Async HTTP Client",
"contributor": "",
"description": "Fixed potential connection leak issue when SDK failed to convert the SDK request to CRT request"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS CRT Async HTTP Client",
"contributor": "",
"description": "Fixed the issue where AWS CRT HTTP client was eagerly buffering data before the underlying CRT component was able to handle it"
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ private void executeRequest(CrtAsyncRequestContext executionContext,
CompletableFuture<Void> requestFuture,
HttpClientConnection crtConn,
AsyncExecuteRequest asyncRequest) {
HttpRequest crtRequest = CrtRequestAdapter.toAsyncCrtRequest(executionContext);
HttpStreamResponseHandler crtResponseHandler =
CrtResponseAdapter.toCrtResponseHandler(crtConn, requestFuture, asyncRequest.responseHandler());

// Submit the request on the connection
try {
HttpRequest crtRequest = CrtRequestAdapter.toAsyncCrtRequest(executionContext);
HttpStreamResponseHandler crtResponseHandler =
CrtResponseAdapter.toCrtResponseHandler(crtConn, requestFuture, asyncRequest.responseHandler());

crtConn.makeRequest(crtRequest, crtResponseHandler).activate();
} catch (HttpException e) {
Throwable toThrow = wrapWithIoExceptionIfRetryable(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.http.crt.internal.request;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.crt.http.HttpRequestBodyStream;
import software.amazon.awssdk.http.async.SdkHttpContentPublisher;
Expand All @@ -26,15 +27,19 @@
final class CrtRequestBodyAdapter implements HttpRequestBodyStream {
private final SdkHttpContentPublisher requestPublisher;
private final ByteBufferStoringSubscriber requestBodySubscriber;
private final AtomicBoolean subscribed = new AtomicBoolean(false);

CrtRequestBodyAdapter(SdkHttpContentPublisher requestPublisher, long readLimit) {
this.requestPublisher = requestPublisher;
this.requestBodySubscriber = new ByteBufferStoringSubscriber(readLimit);
requestPublisher.subscribe(requestBodySubscriber);
}

@Override
public boolean sendRequestBody(ByteBuffer bodyBytesOut) {
if (subscribed.compareAndSet(false, true)) {
requestPublisher.subscribe(requestBodySubscriber);
}

return requestBodySubscriber.transferTo(bodyBytesOut) == TransferResult.END_OF_STREAM;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ public void acquireConnectionThrowException_shouldInvokeOnError() {
assertThat(executeFuture).hasFailedWithThrowableThat().hasCause(exception).isInstanceOf(IOException.class);
}

@Test
public void invalidRequest_requestConversionThrowError_shouldInvokeOnError() {
CrtAsyncRequestContext context = CrtAsyncRequestContext.builder()
.crtConnPool(connectionManager)
.request(AsyncExecuteRequest.builder()
.responseHandler(responseHandler)
.build())
.build();
CompletableFuture<HttpClientConnection> completableFuture = new CompletableFuture<>();

Mockito.when(connectionManager.acquireConnection()).thenReturn(completableFuture);
completableFuture.complete(httpClientConnection);

CompletableFuture<Void> executeFuture = requestExecutor.execute(context);

ArgumentCaptor<Exception> argumentCaptor = ArgumentCaptor.forClass(Exception.class);
Mockito.verify(responseHandler).onError(argumentCaptor.capture());

Exception actualException = argumentCaptor.getValue();
assertThat(actualException).isInstanceOf(NullPointerException.class);
assertThat(executeFuture).hasFailedWithThrowableThat().isInstanceOf(NullPointerException.class);
}

@Test
public void executeAsyncRequest_CrtRuntimeException_shouldInvokeOnError() {
CrtRuntimeException exception = new CrtRuntimeException("");
Expand Down
Loading