[
https://issues.apache.org/jira/browse/GROOVY-12763?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18144663#comment-18144663
]
ASF GitHub Bot commented on GROOVY-12763:
-----------------------------------------
paulk-asert opened a new pull request, #3298:
URL: https://github.com/apache/groovy/pull/3298
Two async fixes for 6.0.1, one commit each.
**GROOVY-12763: async constructs rethrow a checked exception as itself**
`await` rethrew a checked exception unchanged, but five other paths wrapped
it:
- a scope child failure (`DefaultAsyncScope.close()`) and a `defer` action
(`AsyncSupport.executeDeferScope`) in `RuntimeException`
- a `withScope` body (including the timeout variant) and a generator body in
`UndeclaredThrowableException`, added when the closure is converted to
`Function` / `Consumer`
- a `Flow.Publisher` error consumed with `for await`
(`FlowPublisherAdapter`) in `RuntimeException`
All five now rethrow the original exception, as `await` does (GROOVY-9381's
exception-transparency principle; GROOVY-12459 for `for await`). Only the
`UndeclaredThrowableException` layer that closure conversion adds around a
checked exception is removed; other wrappers the code throws are kept.
Suppressed exceptions from scope close are kept.
- `AsyncSupport.rethrow` is now public and returns `RuntimeException` so
call sites can write `throw rethrow(t)`; new `AsyncSupport.unwrapUndeclared`
- `@apiNote` on `AsyncScope` and `close()` for Java callers, which can now
receive a checked exception these methods don't declare
- COMPATIBILITY.md entry for 6.0.1
- `testScopeCloseWithCheckedExceptionWrapped`, which asserted the old
wrapping, becomes `testScopeCloseRethrowsCheckedExceptionUnwrapped`
Known limitation: a checked exception the code itself wrapped in
`UndeclaredThrowableException` and threw from a `withScope` or generator body
is also unwrapped. Removing that needs entry points that avoid closure
conversion (a `Closure` overload of `withScope`, a generator body type
declaring `throws Throwable`), left for a later release.
**GROOVY-12764: no "Self-suppression not permitted" when one exception
instance is reported twice**
Two children (for example, two that await the same failed task) or two
`defer` actions failing with the same instance made `close()` /
`executeDeferScope` throw `IllegalArgumentException` and lose the real failure.
They now skip `addSuppressed` for the same instance, as `withScope` already
does.
**Tests**
New tests in `AsyncAwaitTest` fail before the fix and pass after: each of
the five paths, the two self-suppression cases, the paths under
`@CompileStatic`, and wrapper preservation (a wrapper thrown by a child task or
`defer` action is removed exactly as `await` removes it; one from a
`Flow.Publisher` is kept). Locally: full `./gradlew test` (the one failure was
a race in a new Flow test, since fixed), then `AsyncAwaitTest`,
`groovy.concurrent.*` and `org.apache.groovy.runtime.async.*` green.
Both commits are intended for `GROOVY_6_0_X` after CI passes here.
> Async scopes, defer and generators wrap checked exceptions instead of
> rethrowing them as await does
> ---------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12763
> URL: https://issues.apache.org/jira/browse/GROOVY-12763
> Project: Groovy
> Issue Type: Bug
> Reporter: Paul King
> Priority: Major
>
> {{await}} rethrows a checked exception from an async task unchanged, but five
> other async paths wrap it, so the caller sees a different exception from the
> one the code threw. Unchecked exceptions and {{Error}}s pass through
> unwrapped on every path.
> {code:groovy}
> import groovy.concurrent.*
> // rethrown as is: java.lang.Exception
> await async { throw new Exception('boom') }
> // java.lang.RuntimeException, cause java.lang.Exception
> AsyncScope.withScope { s -> s.async { throw new Exception('boom') }; null }
> // java.lang.reflect.UndeclaredThrowableException, cause java.lang.Exception
> AsyncScope.withScope { s -> throw new Exception('boom') }
> // java.lang.RuntimeException, cause java.lang.Exception
> await async { defer { throw new Exception('boom') }; 1 }
> // java.lang.reflect.UndeclaredThrowableException, cause java.lang.Exception
> def g = async { yield return 1; throw new Exception('boom') }
> for (x in g) {}
> {code}
> The five paths, as of 6.0.0:
> * *Scope child failure*: {{DefaultAsyncScope.close()}} wraps a checked
> failure in {{RuntimeException}}. Its javadoc says "the first failure is
> rethrown".
> * *{{withScope}} body*: the Closure is coerced to {{Function}}, which wraps a
> checked exception in {{UndeclaredThrowableException}}. The same applies to
> {{withScope}} with a timeout.
> * *{{defer}} action*: {{AsyncSupport.executeDeferScope}} wraps it in
> {{RuntimeException}}.
> * *Generator body*: the Closure-to-{{Consumer}} coercion wraps it in
> {{UndeclaredThrowableException}} ({{GeneratorBridge}}).
> * *{{Flow.Publisher}} consumed with {{for await}}*: an {{onError}} carrying a
> checked exception is rethrown as {{RuntimeException}}
> ({{FlowPublisherAdapter}}).
> Expected: the original exception in all five cases, as with {{await}}.
> GROOVY-9381 lists exception transparency as a design principle ("callers see
> the same exceptions they would in synchronous code"), and GROOVY-12459 made a
> failing {{for await}} producer surface its checked exception rather than a
> wrapper. Kotlin's {{coroutineScope}} and C#'s {{await}} also rethrow the
> original exception.
> Proposed fix:
> * Rethrow the original exception at the three {{RuntimeException}} wrap sites.
> * In {{withScope}} and the generator, remove an
> {{UndeclaredThrowableException}} only when its cause is a checked exception,
> which is the layer the closure coercion adds. An
> {{UndeclaredThrowableException}} around an unchecked exception, or another
> wrapper the code itself threw, is kept.
> * Suppressed exceptions from scope close are kept.
> * Add an {{@apiNote}} to {{AsyncScope}} and {{close()}}: both declare no
> checked exceptions but can now throw one, so Java callers catch {{Exception}}
> and test its type.
> A wrapper that a child task or {{defer}} action throws itself, such as
> {{ExecutionException}}, is removed as {{await}} removes it. On 6.0.0 the
> extra {{RuntimeException}} happened to hide that. Whether {{await}} should
> remove only the layers the runtime added is left for a separate discussion.
> Compatibility: code that catches {{RuntimeException}} or
> {{UndeclaredThrowableException}} around these calls and then reads
> {{getCause()}} will now receive the checked exception directly.
> {{AsyncAwaitTest.testScopeCloseWithCheckedExceptionWrapped}} asserted the old
> wrapping and changes with the fix. This needs a release note.
> Known limitation: a checked exception that the code itself wrapped in an
> {{UndeclaredThrowableException}} and threw from a {{withScope}} or generator
> body is also unwrapped, because it can't be told apart from the coercion
> layer. Removing that limitation needs new entry points that avoid closure
> coercion (a generator body type declaring {{throws Throwable}}, and a
> {{Closure}} overload of {{withScope}}); that is left for a later release.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)