Skip to content

Commit

Permalink
Future.await should interrupt the current thread when the worker exec…
Browse files Browse the repository at this point in the history
…utor 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.
  • Loading branch information
vietj committed Nov 6, 2024
1 parent ccca2b8 commit a87afd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/io/vertx/core/Future.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,12 @@ static <T> T await(Future<T> future) {
}
if (future.succeeded()) {
return future.result();
} else {
} else if (future.failed()) {
Utils.throwAsUnchecked(future.cause());
return null;
} else {
Utils.throwAsUnchecked(new InterruptedException("Context closed"));
return null;
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/io/vertx/core/VirtualThreadContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,31 @@ public void testContextCloseContextSerialization() throws Exception {
}
f.toCompletionStage().toCompletableFuture().get(20, TimeUnit.SECONDS);
}

@Test
public void testAwaitWhenClosed() throws Exception {
Assume.assumeTrue(isVirtualThreadAvailable());
ContextInternal ctx = vertx.createVirtualThreadContext();
CountDownLatch latch = new CountDownLatch(1);
ctx.runOnContext(v -> {
latch.countDown();
try {
new CountDownLatch(1).await();
fail();
} catch (InterruptedException expected) {
assertFalse(Thread.currentThread().isInterrupted());
}
try {
Promise.promise().future().await();
fail();
} catch (Exception e) {
assertEquals(InterruptedException.class, e.getClass());
testComplete();
}
});
awaitLatch(latch);
// Interrupts virtual thread
ctx.close();
await();
}
}

0 comments on commit a87afd2

Please # to comment.