You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
There is a synchronization issue in the method com.mashape.unirest.http.HttpClientHelper.requestAsync.
To explain the problem I will post the relevant method code here:
public static <T> Future<HttpResponse<T>> requestAsync(HttpRequest request, final Class<T> responseClass, Callback<T> callback) {
HttpUriRequest requestObj = prepareRequest(request, true);
CloseableHttpAsyncClient asyncHttpClient = ClientFactory.getAsyncHttpClient();
if (!asyncHttpClient.isRunning()) { //ENTER CS
asyncHttpClient.start();
AsyncIdleConnectionMonitorThread asyncIdleConnectionMonitorThread = (AsyncIdleConnectionMonitorThread) Options.getOption(Option.ASYNC_MONITOR);
asyncIdleConnectionMonitorThread.start(); // <-- this line throws ThreadStateException
}
//... rest of the code
}
The problem is that if several threads attempt to perform requests with as*Async operations at the same time then it can happen that two threads will enter if in the line marked with ENTER_CS on the same time start the asyncHttpClient and then the asyncIdleConnectionMonitorThread. Since the asyncIdleConnectionMonitorThread's start method will be called twice - the second time will throw ThreadStateException (starting a thread that is already started).
Here is a simple program that reproduce the bug
public static void main(String[] args) {
int threads = 8;
CountDownLatch letch = new CountDownLatch(1);
for (int i = 0; i < threads; i++) {
new Thread(() -> {
try {
letch.await();
Unirest.get("http://www.google.com").asStringAsync().get();
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(Bug.class.getName()).log(Level.SEVERE, null, ex);
}
}).start();
}
letch.countDown();
}
The text was updated successfully, but these errors were encountered:
From #143
Hello,
There is a synchronization issue in the method com.mashape.unirest.http.HttpClientHelper.requestAsync.
To explain the problem I will post the relevant method code here:
The problem is that if several threads attempt to perform requests with as*Async operations at the same time then it can happen that two threads will enter if in the line marked with ENTER_CS on the same time start the asyncHttpClient and then the asyncIdleConnectionMonitorThread. Since the asyncIdleConnectionMonitorThread's start method will be called twice - the second time will throw ThreadStateException (starting a thread that is already started).
Here is a simple program that reproduce the bug
The text was updated successfully, but these errors were encountered: