-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(gax): remove circular ref between resumable upload future and chunk coordinator #14421
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 all commits
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 |
|---|---|---|
|
|
@@ -128,16 +128,46 @@ public void onSuccess(ResumableUploadSession session) { | |
| ResumableUploadChunkCoordinator<ResponseT> coordinator = | ||
| new ResumableUploadChunkCoordinator<>( | ||
| uploadChunkCallable, | ||
| session.getUploadUrl(), | ||
| uploadSessionUrl, | ||
| payload, | ||
| settings.getChunkSize(), | ||
| callContext, | ||
| ResumableUploadFutureImpl.this); | ||
| callContext); | ||
| ApiFuture<ResponseT> uploadFuture; | ||
| try { | ||
| coordinator.start(); | ||
| uploadFuture = coordinator.start(); | ||
| } catch (Throwable t) { | ||
| fail(t); | ||
| return; | ||
| } | ||
| boolean alreadyDone = false; | ||
| synchronized (lock) { | ||
| if (resultFuture.isDone()) { | ||
| alreadyDone = true; | ||
| } else { | ||
| inFlightFuture = uploadFuture; | ||
| } | ||
| } | ||
| if (alreadyDone) { | ||
| uploadFuture.cancel(true); | ||
| return; | ||
| } | ||
|
Comment on lines
+143
to
153
Contributor
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. With the removal of To fix this, check if boolean shouldCancel = false;
synchronized (lock) {
if (resultFuture.isDone()) {
shouldCancel = resultFuture.isCancelled();
} else {
inFlightFuture = uploadFuture;
}
}
if (shouldCancel) {
uploadFuture.cancel(true);
return;
}References
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. Fixed. |
||
| ApiFutures.addCallback( | ||
| uploadFuture, | ||
| new ApiFutureCallback<ResponseT>() { | ||
| @Override | ||
| public void onSuccess(ResponseT response) { | ||
| succeed(response); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| if (t instanceof CancellationException) { | ||
| return; | ||
| } | ||
| fail(t); | ||
| } | ||
| }, | ||
| MoreExecutors.directExecutor()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -151,33 +181,15 @@ public void onFailure(Throwable t) { | |
| MoreExecutors.directExecutor()); | ||
| } | ||
|
|
||
| /** | ||
| * Registers the active in-flight future for cancellation. If this session future has already been | ||
| * canceled, the supplied future is canceled immediately. | ||
| */ | ||
| void setInFlightFuture(ApiFuture<?> inFlightFuture) { | ||
| boolean shouldCancel = false; | ||
| synchronized (lock) { | ||
| if (resultFuture.isDone()) { | ||
| shouldCancel = resultFuture.isCancelled(); | ||
| } else { | ||
| this.inFlightFuture = inFlightFuture; | ||
| } | ||
| } | ||
| if (shouldCancel) { | ||
| inFlightFuture.cancel(true); | ||
| } | ||
| } | ||
|
|
||
| void succeed(@Nullable ResponseT result) { | ||
| private void succeed(@Nullable ResponseT result) { | ||
| synchronized (lock) { | ||
| inFlightFuture = null; | ||
| } | ||
| closePayload(); | ||
| resultFuture.set(result); | ||
| } | ||
|
|
||
| void fail(Throwable t) { | ||
| private void fail(Throwable t) { | ||
| synchronized (lock) { | ||
| inFlightFuture = null; | ||
| } | ||
|
|
||
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.
There is a race condition where
resultcan be cancelled afterresult.isDone()is checked at the beginning oftransmitChunk, but beforecurrentChunkFutureis assigned. In this scenario, the cancellation listener registered instart()will have already executed (findingcurrentChunkFutureto be null or a previous chunk), and the newly createdchunkFuturewill never be cancelled, leading to a leaked background upload task.To prevent this, check if
resulthas been cancelled immediately after assigningcurrentChunkFutureand cancel the chunk future if so.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.
Fixed.