[ 
https://issues.apache.org/jira/browse/GROOVY-12788?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152746#comment-18152746
 ] 

ASF GitHub Bot commented on GROOVY-12788:
-----------------------------------------

jaragunde opened a new pull request, #3380:
URL: https://github.com/apache/groovy/pull/3380

   The previous implementation relied on CompletableFuture.allOf to wait for 
the Futures to complete, but it isn't actually a fail-fast method.
   
   The new implementation assigns whenComplete callbacks to every Future, and 
calls completeExceptionally on the result Future as soon as one of them 
completes with error.
   
   Adds tests to verify that calls to Awaitable.all and await(a,b) do return 
early when one of the futures fails, even if one of them didn't complete.
   
   Assisted-by: Claude Code (Claude Opus 5.5)




> Awaitable.all() and await(a, b) do not fail fast as documented
> --------------------------------------------------------------
>
>                 Key: GROOVY-12788
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12788
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 6.0.0
>            Reporter: Stephen Albrecht
>            Priority: Major
>
> Environment: Groovy 6.0.0, JDK 25.0.1
> The 
> [JavaDoc|https://github.com/apache/groovy/blob/bc6b3cfd9f8f3397129f8ebbdf85a189d16d7360/src/main/java/groovy/concurrent/Awaitable.java#L402-L404]
>  for Awaitable.all() states "Like JavaScript's Promise.all(), the combined 
> awaitable fails as soon as the first source fails."
> However, Awaitable.all() waits for every source to complete before failing. 
> The implementation in  
> [AwaitCombinators.allAsync|https://github.com/apache/groovy/blob/bc6b3cfd9f8f3397129f8ebbdf85a189d16d7360/src/main/java/org/apache/groovy/runtime/async/AwaitCombinators.java#L84-L85]
>  is built on CompletableFuture.allOf(), whose JavaDoc states "Returns a new 
> CompletableFuture that is completed when all of the given CompletableFutures 
> complete. If any of the given CompletableFutures complete exceptionally, then 
> the returned CompletableFuture also does so". allOf() therefore isn't 
> fail-fast.
> await(a, b) is affected too.
> {code:java}
> import groovy.concurrent.Awaitable
> def time = { String label, Closure body ->
>     def start = System.currentTimeMillis()
>     try {
>         body()
>     } catch (e) {
>         println "$label threw after ${System.currentTimeMillis() - start}ms"
>     }
> }
> time('Awaitable.all') {
>     await Awaitable.all(
>         async { Thread.sleep(3000) },
>         async { throw new RuntimeException('fail') }
>     )
> }
> time('await(a, b)') {
>     await(
>         async { Thread.sleep(3000) },
>         async { throw new RuntimeException('fail') }
>     )
> }
> {code}
> {code:java}
> Awaitable.all threw after 3005ms
> await(a, b) threw after 3004ms{code}
> Expected: both throw after ~0ms (the second source fails immediately)
> Actual: both throw after 3000ms (once the first source completes)
> allAsync() already [hooks a callback onto every source that runs on 
> failure|https://github.com/apache/groovy/blob/bc6b3cfd9f8f3397129f8ebbdf85a189d16d7360/src/main/java/org/apache/groovy/runtime/async/AwaitCombinators.java#L79],
>  so it can probably just return the failure immediately with 
> result.completeExceptionally(asCompletionException(error)) and keep 
> CompletableFuture.allOf() for the success case.
> This would match the Promise.all() behavior which doesn't cancel the 
> remaining sources.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to