Skip to content

Commit

Permalink
Fix bug in WorkRequestHandler's handling of singleplex requests that …
Browse files Browse the repository at this point in the history
…would cause occasional hangs.

RELNOTES: None.
PiperOrigin-RevId: 386194200
  • Loading branch information
larsrc-google authored and copybara-github committed Jul 22, 2021
1 parent 951a302 commit 5e352af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,40 @@ public WorkRequestHandler build() {
* returns. If {@code in} reaches EOF, it also returns.
*/
public void processRequests() throws IOException {
while (true) {
WorkRequest request = messageProcessor.readWorkRequest();
if (request == null) {
break;
}
if (request.getCancel()) {
respondToCancelRequest(request);
} else {
startResponseThread(request);
try {
while (true) {
WorkRequest request = messageProcessor.readWorkRequest();
if (request == null) {
break;
}
if (request.getCancel()) {
respondToCancelRequest(request);
} else {
startResponseThread(request);
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
stderr.println("InterruptedException processing requests.");
}
}

/** Starts a thread for the given request. */
void startResponseThread(WorkRequest request) {
void startResponseThread(WorkRequest request) throws InterruptedException {
Thread currentThread = Thread.currentThread();
String threadName =
request.getRequestId() > 0
? "multiplex-request-" + request.getRequestId()
: "singleplex-request";
// TODO(larsrc): See if this can be handled with a queue instead, without introducing more
// race conditions.
if (request.getRequestId() == 0) {
while (activeRequests.containsKey(request.getRequestId())) {
// b/194051480: Previous singleplex requests can still be in activeRequests for a bit after
// the response has been sent. We need to wait for them to vanish.
Thread.sleep(1);
}
}
Thread t =
new Thread(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ public void processRequests() throws IOException {
if (request.getCancel()) {
respondToCancelRequest(request);
} else {
startResponseThread(request);
try {
startResponseThread(request);
} catch (InterruptedException e) {
// We don't expect interrupts at this level, only inside the individual request
// handling threads, so here we just abort on interrupt.
e.printStackTrace();
return;
}
}
if (workerOptions.exitAfter > 0 && workUnitCounter > workerOptions.exitAfter) {
System.exit(0);
Expand Down

0 comments on commit 5e352af

Please sign in to comment.