-
Notifications
You must be signed in to change notification settings - Fork 916
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -82,8 +98,4 @@ public void cleanUpConnectionBasedOnStatusCode(HttpStream stream) { | |
releaseConnection(stream); | ||
} | ||
} | ||
|
||
public AtomicBoolean connectionClosed() { | ||
return connectionClosed; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.