Skip to content

Commit a87afd2

Browse files
committed
Future.await should interrupt the current thread when the worker executor is closed.
Motivation: Future.await incorrectly performs a no-op when the worker executor is closed (returns a null latch), which reports a failure that might not exist. Changes: When the worker executor returns null, throw an interrupted exception.
1 parent ccca2b8 commit a87afd2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/java/io/vertx/core/Future.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,12 @@ static <T> T await(Future<T> future) {
683683
}
684684
if (future.succeeded()) {
685685
return future.result();
686-
} else {
686+
} else if (future.failed()) {
687687
Utils.throwAsUnchecked(future.cause());
688688
return null;
689+
} else {
690+
Utils.throwAsUnchecked(new InterruptedException("Context closed"));
691+
return null;
689692
}
690693
}
691694

src/test/java/io/vertx/core/VirtualThreadContextTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,31 @@ public void testContextCloseContextSerialization() throws Exception {
349349
}
350350
f.toCompletionStage().toCompletableFuture().get(20, TimeUnit.SECONDS);
351351
}
352+
353+
@Test
354+
public void testAwaitWhenClosed() throws Exception {
355+
Assume.assumeTrue(isVirtualThreadAvailable());
356+
ContextInternal ctx = vertx.createVirtualThreadContext();
357+
CountDownLatch latch = new CountDownLatch(1);
358+
ctx.runOnContext(v -> {
359+
latch.countDown();
360+
try {
361+
new CountDownLatch(1).await();
362+
fail();
363+
} catch (InterruptedException expected) {
364+
assertFalse(Thread.currentThread().isInterrupted());
365+
}
366+
try {
367+
Promise.promise().future().await();
368+
fail();
369+
} catch (Exception e) {
370+
assertEquals(InterruptedException.class, e.getClass());
371+
testComplete();
372+
}
373+
});
374+
awaitLatch(latch);
375+
// Interrupts virtual thread
376+
ctx.close();
377+
await();
378+
}
352379
}

0 commit comments

Comments
 (0)