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

CompletableFuture.orTimeout leaks if the future completes exceptionally

XMLWordPrintable

    • b15
    • generic
    • generic
    • Verified

        ADDITIONAL SYSTEM INFORMATION :
        This issue appears to be in all versions of CompletableFuture

        A DESCRIPTION OF THE PROBLEM :
        CompletableFuture.orTimeout schedules a delayed task which is used to exceptionally complete the future with a TimeoutException if it has not otherwise completed before the timeout expires. If the future completes before the timeout the delayed task is canceled and removed from the delayed executors queue. The bug is that this only occurs if the future was completed non-exceptionally, when completed exceptionally the delayed task is not cancelled and its memory is thus retained until the timeout even though it will no longer serve any purpose. Given that timeouts should be significantly longer than expected completion times this leak could be non-negligible if exceptional completions occur frequently.

        The issue is in CompletableFuture.Canceller.accept, which has the following check:

                    if (ex == null && f != null && !f.isDone())
                        f.cancel(false);

        The "ex==null" appears to be a premature optimization and is the cause of the leak.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Create timeout based CompletableFutures in a loop and complete them exceptionally.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Should run forever with periodic GC but no OOME.

        ---------- BEGIN SOURCE ----------
            while (true) {
              new CompletableFuture<>().orTimeout(1, TimeUnit.HOURS).completeExceptionally(new Exception());
            }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        don't use orTimeout :(

        FREQUENCY : always


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

                Created:
                Updated:
                Resolved: