Skip to content

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

Open
@qwwdfsad

Description

@qwwdfsad

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions