Skip to content

Commit 3743351

Browse files
committed
Fix race condition in FileAsyncResponseTransformer where future can be completed before last bytebuffer is written to the file
1 parent 7eeaf1a commit 3743351

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/async/FileAsyncResponseTransformer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void onNext(ByteBuffer byteBuffer) {
121121
if (byteBuffer == null) {
122122
throw new NullPointerException("Element must not be null");
123123
}
124-
124+
125125
writeInProgress = true;
126126
fileChannel.write(byteBuffer, position.get(), byteBuffer, new CompletionHandler<Integer, ByteBuffer>() {
127127
@Override
@@ -130,7 +130,7 @@ public void completed(Integer result, ByteBuffer attachment) {
130130
position.addAndGet(result);
131131
synchronized (FileSubscriber.this) {
132132
if (closeOnLastWrite) {
133-
invokeSafely(fileChannel::close);
133+
close();
134134
} else {
135135
subscription.request(1);
136136
}
@@ -163,12 +163,16 @@ public void onComplete() {
163163
close();
164164
}
165165
}
166-
future.complete(null);
167166
}
168167

169168
private void close() {
170-
if (fileChannel != null) {
171-
invokeSafely(fileChannel::close);
169+
try {
170+
if (fileChannel != null) {
171+
invokeSafely(fileChannel::close);
172+
}
173+
future.complete(null);
174+
} catch (RuntimeException exception) {
175+
future.completeExceptionally(exception);
172176
}
173177
}
174178

0 commit comments

Comments
 (0)