Skip to content

Commit c534df3

Browse files
authored
Check if stream is closed before trying to increment window. (#4833)
1 parent 9f9a56d commit c534df3

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
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 CRT HTTP Client",
4+
"contributor": "",
5+
"description": "Fixed the issue in the AWS CRT HTTP client where the application could crash if stream.incrementWindow was invoked on a closed stream"
6+
}

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/CrtResponseAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
9595
return;
9696
}
9797

98-
stream.incrementWindow(bodyBytesIn.length);
98+
if (!responseHandlerHelper.connectionClosed().get()) {
99+
stream.incrementWindow(bodyBytesIn.length);
100+
}
99101
});
100102

101103
return 0;

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/InputStreamAdaptingHttpStreamResponseHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
9292
return;
9393
}
9494

95-
// increment the window upon buffer consumption.
96-
stream.incrementWindow(bodyBytesIn.length);
95+
if (!responseHandlerHelper.connectionClosed().get()) {
96+
// increment the window upon buffer consumption.
97+
stream.incrementWindow(bodyBytesIn.length);
98+
}
9799
});
98100

99101
// Window will be incremented after the subscriber consumes the data, returning 0 here to disable it.

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/ResponseHandlerHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,8 @@ public void cleanUpConnectionBasedOnStatusCode(HttpStream stream) {
8282
releaseConnection(stream);
8383
}
8484
}
85+
86+
public AtomicBoolean connectionClosed() {
87+
return connectionClosed;
88+
}
8589
}

http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/internal/BaseHttpStreamResponseHandlerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
package software.amazon.awssdk.http.crt.internal;
1717

1818
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
import static org.mockito.ArgumentMatchers.anyInt;
1920
import static org.mockito.Mockito.never;
2021
import static org.mockito.Mockito.verify;
2122

23+
import java.nio.charset.StandardCharsets;
2224
import java.util.concurrent.CompletableFuture;
2325
import org.junit.jupiter.api.BeforeEach;
2426
import org.junit.jupiter.api.Test;
@@ -95,6 +97,22 @@ void failedToGetResponse_shouldShutdownConnection() {
9597
verify(httpStream).close();
9698
}
9799

100+
@Test
101+
void streamClosed_shouldNotIncreaseStreamWindow() throws InterruptedException {
102+
HttpHeader[] httpHeaders = getHttpHeaders();
103+
responseHandler.onResponseHeaders(httpStream, 500, HttpHeaderBlock.MAIN.getValue(),
104+
httpHeaders);
105+
responseHandler.onResponseHeadersDone(httpStream, 0);
106+
responseHandler.onResponseBody(httpStream, "{}".getBytes(StandardCharsets.UTF_8));
107+
108+
responseHandler.onResponseComplete(httpStream, 0);
109+
requestFuture.join();
110+
verify(crtConn).shutdown();
111+
verify(crtConn).close();
112+
verify(httpStream).close();
113+
verify(httpStream, never()).incrementWindow(anyInt());
114+
}
115+
98116
private static HttpHeader[] getHttpHeaders() {
99117
HttpHeader[] httpHeaders = new HttpHeader[1];
100118
httpHeaders[0] = new HttpHeader("Content-Length", "1");

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
<rxjava.version>2.2.21</rxjava.version>
120120
<commons-codec.verion>1.15</commons-codec.verion>
121121
<jmh.version>1.29</jmh.version>
122-
<awscrt.version>0.29.2</awscrt.version>
122+
<awscrt.version>0.29.7</awscrt.version>
123123

124124
<!--Test dependencies -->
125125
<junit5.version>5.10.0</junit5.version>

0 commit comments

Comments
 (0)