Skip to content

Commit

Permalink
Gracefully handle calling make_meta_request on closed S3 client (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 authored Dec 14, 2023
1 parent e50af06 commit e68c96a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crt/aws-c-s3
10 changes: 8 additions & 2 deletions src/main/java/software/amazon/awssdk/crt/s3/S3Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,22 @@ private void onShutdownComplete() {

public S3MetaRequest makeMetaRequest(S3MetaRequestOptions options) {

if(isNull()) {
Log.log(Log.LogLevel.Error, Log.LogSubject.S3Client,
"S3Client.makeMetaRequest has invalid client. The client can not be used after it is closed.");
throw new IllegalStateException("S3Client.makeMetaRequest has invalid client. The client can not be used after it is closed.");
}

if (options.getHttpRequest() == null) {
Log.log(Log.LogLevel.Error, Log.LogSubject.S3Client,
"S3Client.makeMetaRequest has invalid options; Http Request cannot be null.");
return null;
throw new IllegalArgumentException("S3Client.makeMetaRequest has invalid options; Http Request cannot be null.");
}

if (options.getResponseHandler() == null) {
Log.log(Log.LogLevel.Error, Log.LogSubject.S3Client,
"S3Client.makeMetaRequest has invalid options; Response Handler cannot be null.");
return null;
throw new IllegalArgumentException("S3Client.makeMetaRequest has invalid options; Response Handler cannot be null.");
}

S3MetaRequest metaRequest = new S3MetaRequest();
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/software/amazon/awssdk/crt/test/S3ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,36 @@ public void onFinished(S3FinishedResponseContext context) {
}
}

@Test
public void testS3GetAfterClientIsClose() {
skipIfAndroid();
skipIfNetworkUnavailable();
Assume.assumeTrue(hasAwsCredentials());
S3ClientOptions clientOptions = new S3ClientOptions().withRegion(REGION);
S3Client client = createS3Client(clientOptions);
client.close();
S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

@Override
public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long objectRangeEnd) {
return 0;
}

@Override
public void onFinished(S3FinishedResponseContext context) {
}
};

HttpHeader[] headers = { new HttpHeader("Host", ENDPOINT) };
HttpRequest httpRequest = new HttpRequest("GET", "/get_object_test_1MB.txt", headers, null);

S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions()
.withMetaRequestType(MetaRequestType.GET_OBJECT).withHttpRequest(httpRequest)
.withResponseHandler(responseHandler);

assertThrows(IllegalStateException.class, () -> client.makeMetaRequest(metaRequestOptions));
}

@Test
public void testS3CallbackExceptionIsProperlyPropagated() {
skipIfAndroid();
Expand Down

0 comments on commit e68c96a

Please sign in to comment.