-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix request cancellation issue in the AWS CRT-based S3 client that co… #4955
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "AWS CRT-based S3 client", | ||
| "contributor": "", | ||
| "description": "Fixed memory leak issue when a request was cancelled in the AWS CRT-based S3 client." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,25 +18,24 @@ | |
| import java.util.function.Function; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.crt.s3.ResumeToken; | ||
| import software.amazon.awssdk.crt.s3.S3MetaRequest; | ||
|
|
||
| /** | ||
| * An observable that notifies the observer {@link S3CrtAsyncHttpClient} to pause the request. | ||
| */ | ||
| @SdkInternalApi | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class should've been a protected API. I will create a separate PR to fix it. |
||
| public class S3MetaRequestPauseObservable { | ||
|
|
||
| private final Function<S3MetaRequest, ResumeToken> pause; | ||
| private volatile S3MetaRequest request; | ||
| private final Function<S3MetaRequestWrapper, ResumeToken> pause; | ||
| private volatile S3MetaRequestWrapper request; | ||
|
|
||
| public S3MetaRequestPauseObservable() { | ||
| this.pause = S3MetaRequest::pause; | ||
| this.pause = S3MetaRequestWrapper::pause; | ||
| } | ||
|
|
||
| /** | ||
| * Subscribe {@link S3MetaRequest} to be potentially paused later. | ||
| * Subscribe {@link S3MetaRequestWrapper} to be potentially paused later. | ||
| */ | ||
| public void subscribe(S3MetaRequest request) { | ||
| public void subscribe(S3MetaRequestWrapper request) { | ||
| this.request = request; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.services.s3.internal.crt; | ||
|
|
||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.crt.s3.ResumeToken; | ||
| import software.amazon.awssdk.crt.s3.S3MetaRequest; | ||
|
|
||
| /** | ||
| * A wrapper class that manages the lifecycle of the underlying {@link S3MetaRequest}. This class is needed to ensure we don't | ||
| * invoke methods on {@link S3MetaRequest} after it's closed, otherwise CRT will crash. | ||
| */ | ||
| @SdkInternalApi | ||
| public class S3MetaRequestWrapper { | ||
| private final S3MetaRequest delegate; | ||
| private volatile boolean isClosed; | ||
| private final Object lock = new Object(); | ||
|
|
||
| public S3MetaRequestWrapper(S3MetaRequest delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| public void close() { | ||
| synchronized (lock) { | ||
| if (!isClosed) { | ||
| isClosed = true; | ||
| delegate.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void incrementReadWindow(long windowSize) { | ||
| synchronized (lock) { | ||
| if (!isClosed) { | ||
| delegate.incrementReadWindow(windowSize); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public ResumeToken pause() { | ||
| synchronized (lock) { | ||
| if (!isClosed) { | ||
| return delegate.pause(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public void cancel() { | ||
| synchronized (lock) { | ||
| if (!isClosed) { | ||
| delegate.cancel(); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,10 +64,12 @@ | |
| import software.amazon.awssdk.services.s3.model.Protocol; | ||
| import software.amazon.awssdk.services.s3.model.S3Exception; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
| import software.amazon.awssdk.utils.Logger; | ||
| import software.amazon.awssdk.utils.http.SdkHttpUtils; | ||
|
|
||
| @WireMockTest(httpsEnabled = true) | ||
| public class S3ExpressCreateSessionTest extends BaseRuleSetClientTest { | ||
| private static final Logger log = Logger.loggerFor(S3ExpressCreateSessionTest.class); | ||
|
|
||
| private static final Function<WireMockRuntimeInfo, URI> WM_HTTP_ENDPOINT = wm -> URI.create(wm.getHttpBaseUrl()); | ||
| private static final Function<WireMockRuntimeInfo, URI> WM_HTTPS_ENDPOINT = wm -> URI.create(wm.getHttpsBaseUrl()); | ||
|
|
@@ -329,9 +331,8 @@ private static final class CapturingInterceptor implements ExecutionInterceptor | |
| public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttributes executionAttributes) { | ||
| SdkHttpRequest sdkHttpRequest = context.httpRequest(); | ||
| this.headers = sdkHttpRequest.headers(); | ||
| System.out.printf("%s %s%n", sdkHttpRequest.method(), sdkHttpRequest.encodedPath()); | ||
| headers.forEach((k, strings) -> System.out.printf("%s, %s%n", k, strings)); | ||
| System.out.println(); | ||
| log.debug(() -> String.format("%s %s%n", sdkHttpRequest.method(), sdkHttpRequest.encodedPath())); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this because I noticed it produced a lot of (unnecessary) logs. |
||
| headers.forEach((k, strings) -> log.debug(() -> String.format("%s, %s%n", k, strings))); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this to S3CrtResponseHandlerAdapter because I figured it makes more sense to manage the closure of the S3MetaRequest in one place.