Skip to content

Commit 95c06b1

Browse files
dfa1claude
andcommitted
fix(cli): decrement pending before signaling in IoWorker.runAndAwait
runAndAwait waited on a signal raised in the task's finally, while pending was decremented later in submit's own finally — so a caller reading pending() right after runAndAwait returned could still see the task counted. Replace the signal with a CountDownLatch counted down after the decrement, making the decrement happen-before the await returns. Also bail out early when closed, matching submit and avoiding an indefinite wait on a closed worker. Fixes flaky IoWorkerTest.pending_isZeroAfterTaskCompletes (reproduced on CI). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 35436f1 commit 95c06b1

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

cli/src/main/java/io/github/dfa1/vortex/cli/tui/IoWorker.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.dfa1.vortex.cli.tui;
22

33
import java.util.concurrent.BlockingQueue;
4+
import java.util.concurrent.CountDownLatch;
45
import java.util.concurrent.LinkedBlockingQueue;
56
import java.util.concurrent.atomic.AtomicInteger;
67

@@ -57,23 +58,22 @@ public void submit(Runnable task) {
5758
/// @param task task to execute
5859
/// @throws InterruptedException if the calling thread is interrupted while waiting
5960
public void runAndAwait(Runnable task) throws InterruptedException {
60-
Object signal = new Object();
61-
boolean[] done = {false};
62-
submit(() -> {
61+
if (closed) {
62+
return;
63+
}
64+
// Count down only after pending is decremented, so a caller that reads pending()
65+
// immediately after this returns never observes the in-flight task still counted.
66+
CountDownLatch done = new CountDownLatch(1);
67+
pending.incrementAndGet();
68+
queue.add(() -> {
6369
try {
6470
task.run();
6571
} finally {
66-
synchronized (signal) {
67-
done[0] = true;
68-
signal.notifyAll();
69-
}
72+
pending.decrementAndGet();
73+
done.countDown();
7074
}
7175
});
72-
synchronized (signal) {
73-
while (!done[0]) {
74-
signal.wait();
75-
}
76-
}
76+
done.await();
7777
}
7878

7979
/// Number of submitted tasks that have not yet finished.

0 commit comments

Comments
 (0)