Skip to content

Commit c93c3d2

Browse files
committed
Fix processQueue concurrent execution resulting in too many concurrent requests
processQueue may be invoked from multiple threads as requests complete or new requests are submitted. The check of `inProcess.size()` and inProcess.put are not atomic, meaning multiple threads may see capacity then overflow that capacity with new requests. This fix adds a synchronized block. A non-blocking ReschedulingOperation solution was also considered, but in theory this function should block for a version short period, making simple synchronization potentially a better option.
1 parent dad1ca2 commit c93c3d2

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

client/src/main/java/org/threadly/litesockets/client/http/HTTPClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,11 @@ public ListenableFuture<HTTPResponseData> requestAsync(final ClientHTTPRequest r
370370

371371
protected void processQueue() {
372372
//This should be done after we do a .select on the ntse to check for more jobs before it exits.
373-
HTTPRequestWrapper hrw;
374-
while(maxConcurrent > inProcess.size() && (hrw = queue.poll()) != null) {
375-
process(hrw);
373+
synchronized (inProcess) {
374+
HTTPRequestWrapper hrw;
375+
while(maxConcurrent > inProcess.size() && (hrw = queue.poll()) != null) {
376+
process(hrw);
377+
}
376378
}
377379
}
378380

0 commit comments

Comments
 (0)