Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8331987

Enhance stacktrace clarity for CompletableFuture CancellationException

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      When a CompletableFuture is canceled, the resulting CancellationException only points to the location where cancel() was called, not where the exception is thrown from get(). This can obscure the diagnosis of issues in complex applications.
       public static void main(String[] args) {
              var objectCompletableFuture = new CompletableFuture<Object>();
              var thread1 = new Thread(() -> {
                  m1(objectCompletableFuture);
              });
              var thread2 = new Thread(() -> {
                  objectCompletableFuture.cancel(true);
              });
              thread1.start();
              thread2.start();
          }

          private static void m1(CompletableFuture<Object> objectCompletableFuture) {
              m2(objectCompletableFuture);
          }

          private static void m2(CompletableFuture<Object> objectCompletableFuture) {
              try {
                  Thread.sleep(100);
                  objectCompletableFuture.get();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }

      Actually result:
      java.util.concurrent.CancellationException
      at java.base/java.util.concurrent.CompletableFuture.cancel(CompletableFuture.java:2478)
      at org.example.App.lambda$main$1(App.java:13)
      at java.base/java.lang.Thread.run(Thread.java:833)

      Expected result:
      java.util.concurrent.CancellationException
      at org.example.App1.m2(App1.java:26)
      at org.example.App1.m1(App1.java:20)
      at org.example.App1.lambda$main$0(App1.java:10)
      at java.base/java.lang.Thread.run(Thread.java:833)
      Caused by: java.util.concurrent.CancellationException
      at org.example.App1.lambda$main$1(App1.java:13)
      ... 1 more


            vklang Viktor Klang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: