diff --git a/src/main/java/software/amazon/awssdk/crt/s3/S3MetaRequest.java b/src/main/java/software/amazon/awssdk/crt/s3/S3MetaRequest.java index 967b25471..7824bac78 100644 --- a/src/main/java/software/amazon/awssdk/crt/s3/S3MetaRequest.java +++ b/src/main/java/software/amazon/awssdk/crt/s3/S3MetaRequest.java @@ -38,6 +38,13 @@ protected boolean canReleaseReferencesImmediately() { @Override protected void releaseNativeHandle() { if (!isNull()) { + /** + * Cancel the meta request before drop the refcount. + * The meta request is not referenced by Java any longer, everything from native to Java will be ignored. + * Cancelling the meta request instead of letting it keep flowing. + * Note: If the meta request has not finished yet, it will be finished with `AWS_ERROR_S3_CANCELED`. + **/ + s3MetaRequestCancel(getNativeHandle()); s3MetaRequestDestroy(getNativeHandle()); } } diff --git a/src/test/java/software/amazon/awssdk/crt/test/S3ExpressCredentialsProviderHandlerSample.java b/src/test/java/software/amazon/awssdk/crt/test/S3ExpressCredentialsProviderHandlerSample.java index 4518ab272..1772d0fe6 100644 --- a/src/test/java/software/amazon/awssdk/crt/test/S3ExpressCredentialsProviderHandlerSample.java +++ b/src/test/java/software/amazon/awssdk/crt/test/S3ExpressCredentialsProviderHandlerSample.java @@ -67,8 +67,10 @@ public void onFinished(S3FinishedResponseContext context) { .withMetaRequestType(MetaRequestType.DEFAULT).withHttpRequest(httpRequest) .withResponseHandler(responseHandler).withSigningConfig(config); - try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) { - } + S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions); + future.whenComplete((r,t) -> { + metaRequest.close(); + }); return future; }