Skip to content

Commit

Permalink
Fix an issue that could result in resource leak when sending request …
Browse files Browse the repository at this point in the history
…fails due to errors such as invalid request. (#3719)
  • Loading branch information
zoewangg authored Jan 26, 2023
1 parent d66db22 commit ee51283
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSCRTHTTPClient-3f05324.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS CRT HTTP Client",
"contributor": "",
"description": "Fix an issue that could result in resource leak when sending request fails due to errors such as invalid request."
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public CompletableFuture<Void> execute(CrtRequestContext executionContext) {

// If we didn't get a connection for some reason, fail the request
if (throwable != null) {
reportFailure(new IOException("An exception occurred when acquiring a connection", throwable),
reportFailure(crtConn,
new IOException("An exception occurred when acquiring a connection", throwable),
requestFuture,
asyncRequest.responseHandler());
return;
Expand Down Expand Up @@ -121,12 +122,13 @@ private void executeRequest(CrtRequestContext executionContext,
// it's semantically an IOException anyway.
toThrow = new IOException(e);
}
reportFailure(toThrow,
reportFailure(crtConn,
toThrow,
requestFuture,
asyncRequest.responseHandler());
} catch (IllegalStateException | CrtRuntimeException e) {
// CRT throws IllegalStateException if the connection is closed
reportFailure(new IOException("An exception occurred when making the request", e),
reportFailure(crtConn, new IOException("An exception occurred when making the request", e),
requestFuture,
asyncRequest.responseHandler());
}
Expand Down Expand Up @@ -156,9 +158,14 @@ private CompletableFuture<Void> createExecutionFuture(AsyncExecuteRequest reques
/**
* Notify the provided response handler and future of the failure.
*/
private void reportFailure(Throwable cause,
private void reportFailure(HttpClientConnection crtConn,
Throwable cause,
CompletableFuture<Void> executeFuture,
SdkAsyncHttpResponseHandler responseHandler) {
if (crtConn != null) {
crtConn.close();
}

try {
responseHandler.onError(cause);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.crt.CrtResource;
import software.amazon.awssdk.crt.Log;
import software.amazon.awssdk.http.SdkAsyncHttpClientH1TestSuite;
Expand Down Expand Up @@ -47,9 +46,4 @@ protected SdkAsyncHttpClient setupClient() {
return AwsCrtAsyncHttpClient.builder()
.buildWithDefaults(AttributeMap.builder().put(TRUST_ALL_CERTIFICATES, true).build());
}

@Test
// TODO: Remove this override. Temporarily disabling test because it's causing memory leak
public void naughtyHeaderCharactersDoNotGetToServer() {
}
}

0 comments on commit ee51283

Please sign in to comment.