Kotlin/kotlinx.coroutines

Provide an API to recover stacktrace of the exception when it does not cross coroutine boundary

Aperta

#2607 aperta il 24 mar 2021

 (2 commenti) (13 reazioni) (1 assegnatario)Kotlin (1927 fork)batch import
debugdesignhelp wanteduse-case needed

Metriche repository

Star
 (13.749 stelle)
Metriche merge PR
 (Merge medio 130g 3h) (4 PR mergiate in 30 g)

Descrizione

kotlinx.coroutines provides a stacktrace recovery machinery in order to provide complete stacktraces that preserve information about all suspended functions in the stracktrace.

But the exception recovery only works through coroutine boundaries.

For example, the following code:

suspend fun foo() {
    yield() // Suspend
    bar()
    yield()
}

suspend fun bar() {
    yield() // Suspend
    throw IllegalStateException()
}

runBlocking { // Could've been 'launch' or any other coroutine builder
    foo()
}

will be missing foo function from the stacktrace because IllegalStateException does not cross any suspendable coroutine boundaries and stacktrace recovery machinery don't have a chance to kick in.

Instead, we could've provided a new top-level suspend function that recovers the stacktrace. Library authors and application developers could've been using that along with stacktrace-sensitive exceptions and/or "expected" failures.

There is a lot of shape for such API, e.g. throw IllegalStateException().recoverStacktrace() or

recovering {
    require(myArg >= 1, "Expected myArg to be >= 1")
}

but the biggest question of this issue is whether it is useful for other developers. Opinions regarding usefulness of such API are welcomed

Guida contributor