Skip to content

Commit f744b6a

Browse files
[8.1.0] Fix a bug which makes WorkerPool create too many workers and go above the limitation. (#25240)
PiperOrigin-RevId: 725188342 Change-Id: I650741289e906f3d81c95b9956a1fab9603b6ea7 Commit 815b19d Co-authored-by: Googler <[email protected]>
1 parent 14219c4 commit f744b6a

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/main/java/com/google/devtools/build/lib/worker/WorkerPoolImpl.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,13 @@ private Worker borrowWorker(WorkerKey key) throws IOException, InterruptedExcept
291291
while (!idleWorkers.isEmpty()) {
292292
// LIFO: It's better to re-use a worker as often as possible and keep it hot, in order to
293293
// profit from JIT optimizations as much as possible.
294-
worker = idleWorkers.takeLast();
294+
// This cannot be null because we already checked that the queue is not empty.
295+
worker = idleWorkers.peekLast();
295296
// We need to validate with the passed in `key` rather than `worker.getWorkerKey()`
296297
// because the former can contain a different combined files hash if the files changed.
297298
if (factory.validateWorker(key, worker)) {
298299
acquired.incrementAndGet();
300+
idleWorkers.remove(worker);
299301
break;
300302
}
301303
invalidateWorker(worker, /* shouldShrinkPool= */ false);
@@ -335,7 +337,14 @@ private synchronized void returnWorker(WorkerKey key, Worker worker) {
335337
return;
336338
}
337339

338-
activeSet.remove(worker);
340+
if (activeSet.contains(worker)) {
341+
activeSet.remove(worker);
342+
} else {
343+
throw new IllegalStateException(
344+
String.format(
345+
"Worker %s (id %d) is not in the active set",
346+
worker.getWorkerKey().getMnemonic(), worker.getWorkerId()));
347+
}
339348

340349
PendingWorkerRequest pendingReq = waitingQueue.poll();
341350
if (pendingReq != null) {
@@ -356,7 +365,14 @@ private synchronized void invalidateWorker(Worker worker, boolean shouldShrinkPo
356365
}
357366

358367
// If it isn't idle, then we're destroying an active worker.
359-
activeSet.remove(worker);
368+
if (activeSet.contains(worker)) {
369+
activeSet.remove(worker);
370+
} else {
371+
throw new IllegalStateException(
372+
String.format(
373+
"Worker %s (id %d) is not in the active set",
374+
worker.getWorkerKey().getMnemonic(), worker.getWorkerId()));
375+
}
360376

361377
// We don't want to shrink the pool to 0.
362378
if (shouldShrinkPool && getEffectiveMax() > 1) {

0 commit comments

Comments
 (0)