Skip to content

Commit cd870e9

Browse files
authored
Fixed an issue that could cause exception to be thrown from CombinedResponseAsyncHttpResponseHandler#onStream when onError got invoked first (#3603)
1 parent 7fba084 commit cd870e9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Fixed an issue that could cause exception to be thrown from `CombinedResponseAsyncHttpResponseHandler#onStream` when `onError` got invoked first"
6+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public void onStream(Publisher<ByteBuffer> publisher) {
7575
Validate.isTrue(headersFuture != null, "onStream() invoked without prepare().");
7676
Validate.isTrue(headersFuture.isDone(), "headersFuture is still not completed when onStream() is "
7777
+ "invoked.");
78+
79+
if (headersFuture.isCompletedExceptionally()) {
80+
return;
81+
}
82+
7883
SdkHttpResponse sdkHttpResponse = headersFuture.join();
7984
if (sdkHttpResponse.isSuccessful()) {
8085
successResponseHandler.onStream(publisher);

core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/http/async/CombinedResponseAsyncHttpResponseHandlerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
import static org.mockito.Mockito.times;
2021
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.verifyNoInteractions;
23+
import static org.mockito.Mockito.verifyNoMoreInteractions;
2124
import static org.mockito.Mockito.when;
2225

2326
import io.reactivex.Flowable;
@@ -72,6 +75,18 @@ void onStream_invokedWithoutOnHeaders_shouldThrowException() {
7275

7376
}
7477

78+
@Test
79+
void onStream_HeadersFutureCompleteSuccessfully_shouldNotThrowException() {
80+
when(successResponseHandler.prepare()).thenReturn(CompletableFuture.completedFuture(null));
81+
82+
responseHandler.prepare();
83+
responseHandler.onError(new RuntimeException("error"));
84+
Flowable<ByteBuffer> publisher = publisher();
85+
responseHandler.onStream(publisher);
86+
verify(successResponseHandler, times(0)).onStream(publisher);
87+
verify(errorResponseHandler, times(0)).onStream(publisher);
88+
}
89+
7590
@Test
7691
void successResponse_shouldCompleteHeaderFuture() {
7792
when(successResponseHandler.prepare()).thenReturn(CompletableFuture.completedFuture(null));

0 commit comments

Comments
 (0)