Skip to content

ListenableFuture: Exception thrown when a future was cancelled seems to be lost #6794

Open
@YutaHiguchi-bsn

Description

@YutaHiguchi-bsn

When a ListenableFuture is canceled and the interrupted task threw an exception,
exception thrown from the task does not seem to appear in the CancellationException as cause or as suppressed exception.

CountDownLatch latch = new CountDownLatch(1);
ListenableFuture<Void> future = Futures.submit(() -> {
    try {
        latch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException("Cancellation failed", e);
    }
}, ForkJoinPool.commonPool());
Thread.sleep(1000);
future.cancel(true);
CancellationException e = assertThrows(CancellationException.class, future::get);
// expected either e.getCause() or e.getSuppressed() will carry IllegalStateException thrown from Interrupt handler

Do I have a wrong expectation and is there another way to propagate failure during cancellation to the future?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions