Kotlin/kotlinx.coroutines

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

Open

#2607 opened on Mar 24, 2021

View on GitHub
 (2 comments) (13 reactions) (1 assignee)Kotlin (13,749 stars) (1,927 forks)batch import
debugdesignhelp wanteduse-case needed

Description

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

Contributor guide