Skip to content

Commit 3e64224

Browse files
authored
Fixed an issue that could result in uncompletable future when headObject request threw exception in copy (#3609)
1 parent b257950 commit 3e64224

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/crt/CopyObjectHelper.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,23 @@ public CompletableFuture<CopyObjectResponse> copyObject(CopyObjectRequest copyOb
6767

6868
CompletableFuture<CopyObjectResponse> returnFuture = new CompletableFuture<>();
6969

70-
CompletableFuture<HeadObjectResponse> headFuture =
71-
s3AsyncClient.headObject(CopyRequestConversionUtils.toHeadObjectRequest(copyObjectRequest));
72-
73-
// Ensure cancellations are forwarded to the head future
74-
CompletableFutureUtils.forwardExceptionTo(returnFuture, headFuture);
75-
76-
headFuture.whenComplete((headObjectResponse, throwable) -> {
77-
if (throwable != null) {
78-
handleException(returnFuture, () -> "Failed to retrieve metadata from the source object", throwable);
79-
} else {
80-
doCopyObject(copyObjectRequest, returnFuture, headObjectResponse);
81-
}
82-
});
70+
try {
71+
CompletableFuture<HeadObjectResponse> headFuture =
72+
s3AsyncClient.headObject(CopyRequestConversionUtils.toHeadObjectRequest(copyObjectRequest));
73+
74+
// Ensure cancellations are forwarded to the head future
75+
CompletableFutureUtils.forwardExceptionTo(returnFuture, headFuture);
76+
77+
headFuture.whenComplete((headObjectResponse, throwable) -> {
78+
if (throwable != null) {
79+
handleException(returnFuture, () -> "Failed to retrieve metadata from the source object", throwable);
80+
} else {
81+
doCopyObject(copyObjectRequest, returnFuture, headObjectResponse);
82+
}
83+
});
84+
} catch (Throwable throwable) {
85+
returnFuture.completeExceptionally(throwable);
86+
}
8387

8488
return returnFuture;
8589
}

services/s3/src/test/java/software/amazon/awssdk/services/s3/internal/crt/CopyObjectHelperTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ void copyObject_HeadObjectRequestFailed_shouldFail() {
8686
.hasRootCause(exception);
8787
}
8888

89+
@Test
90+
void copyObject_HeadObjectRequestThrowsException_shouldFail() {
91+
RuntimeException exception = new RuntimeException("oops");
92+
93+
when(s3AsyncClient.headObject(any(HeadObjectRequest.class)))
94+
.thenThrow(exception);
95+
96+
CompletableFuture<CopyObjectResponse> future =
97+
copyHelper.copyObject(copyObjectRequest());
98+
99+
assertThatThrownBy(future::join).hasCause(exception);
100+
}
101+
89102
@Test
90103
void singlePartCopy_happyCase_shouldSucceed() {
91104

0 commit comments

Comments
 (0)