Skip to content

Fixed a thread safety issue that could cause application to crash in … #4839

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

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSCRTHTTPClient-7b95a65.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS CRT HTTP Client",
"contributor": "",
"description": "Fixed a thread safety issue that could cause application to crash in the edge case where the SDK attempted to invoke `incrementWindow` after the stream is closed in AWS CRT HTTP Client."
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
return;
}

if (!responseHandlerHelper.connectionClosed().get()) {
stream.incrementWindow(bodyBytesIn.length);
}
responseHandlerHelper.incrementWindow(stream, bodyBytesIn.length);
});

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
failFutureAndCloseConnection(stream, failure);
return;
}

if (!responseHandlerHelper.connectionClosed().get()) {
// increment the window upon buffer consumption.
stream.incrementWindow(bodyBytesIn.length);
}
// increment the window upon buffer consumption.
responseHandlerHelper.incrementWindow(stream, bodyBytesIn.length);
});

// Window will be incremented after the subscriber consumes the data, returning 0 here to disable it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
*
* CRT connection will only be closed, i.e., not reused, in one of the following conditions:
* 1. 5xx server error OR
* 2. It fails to read the response.
* 2. It fails to read the response OR
* 3. the response stream is closed/aborted by the caller.
*/
@SdkInternalApi
public class ResponseHandlerHelper {

private final SdkHttpResponse.Builder responseBuilder;
private final HttpClientConnection connection;
private AtomicBoolean connectionClosed = new AtomicBoolean(false);
private boolean connectionClosed;
private final Object lock = new Object();

public ResponseHandlerHelper(SdkHttpResponse.Builder responseBuilder, HttpClientConnection connection) {
this.responseBuilder = responseBuilder;
Expand All @@ -57,20 +59,34 @@ public void onResponseHeaders(HttpStream stream, int responseStatusCode, int hea
* Release the connection back to the pool so that it can be reused.
*/
public void releaseConnection(HttpStream stream) {
if (connectionClosed.compareAndSet(false, true)) {
connection.close();
stream.close();
synchronized (lock) {
if (!connectionClosed) {
connectionClosed = true;
connection.close();
stream.close();
}
}
}

public void incrementWindow(HttpStream stream, int windowSize) {
Copy link
Contributor

@joviegas joviegas Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please confirm if we have tested and confirmed that there is not latency because of this synchronized check in multi threaded environment with multiple requests going on at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't anticipate any latency change as part of this change since most of times, there should only be one thread accessing those methods. The only exception is that when the request fails, it may be possible one thread is trying to invoke incrementWindow and the other is closing it.

I'll double check our benchmarks after today's release.

synchronized (lock) {
if (!connectionClosed) {
stream.incrementWindow(windowSize);
}
}
}

/**
* Close the connection completely
*/
public void closeConnection(HttpStream stream) {
if (connectionClosed.compareAndSet(false, true)) {
connection.shutdown();
connection.close();
stream.close();
synchronized (lock) {
if (!connectionClosed) {
connectionClosed = true;
connection.shutdown();
connection.close();
stream.close();
}
}
}

Expand All @@ -82,8 +98,4 @@ public void cleanUpConnectionBasedOnStatusCode(HttpStream stream) {
releaseConnection(stream);
}
}

public AtomicBoolean connectionClosed() {
return connectionClosed;
}
}