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

CompletableFuture never completes when 'Throwable.toString()' method throws Exception

XMLWordPrintable

    • b26
    • generic
    • generic
    • Verified

      A DESCRIPTION OF THE PROBLEM :
      When a task within a CompletableFuture encounters an exception and throws a Throwable or Exception object with a faulty 'toString' method, which itself throws another Exception, a cascading failure occurs that causes the CompletableFuture to enter a state where it never completes.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run following code:
      public static void main(String[] args) {
                  CompletableFuture.supplyAsync(() -> {throw new RuntimeException() { @Override public String getMessage() {throw new IllegalArgumentException("getMessage");}};}).join();
              }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An error is thrown by the 'CompletableFuture.join()' call, preventing this code from hanging forever.
      ACTUAL -
      The 'CompletableFuture.join()' call never returns or throws an error.

      ---------- BEGIN SOURCE ----------
      public class Test {
              public static void main(String[] args) {
                  var f0 = CompletableFuture.supplyAsync(() -> {
                      throw new RuntimeException() {
                          @Override public String getMessage() {
                              throw new IllegalArgumentException("getMessage");
                          }
                      };
                  });
                  var f1 = CompletableFuture.supplyAsync(() -> {
                      throw new RuntimeException() {
                          @Override public String toString() {
                              throw new IllegalStateException("toString");
                          }
                      };
                  });
                  // Never completes
                  CompletableFuture.anyOf(f0, f1).join();
              }
          }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: