Stephen Albrecht created GROOVY-12788:
-----------------------------------------

             Summary: 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


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