kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

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

Open qwwdfsad opened this issue 4 years ago • 2 comments

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

qwwdfsad avatar Mar 24 '21 15:03 qwwdfsad

In IJ we often log an error trace and proceed without throwing, this would be a huge help to have the ability to recover full trace.

We'd like to obtain “trace when caught” at the point of “trace when thrown” in the following example:

package com.foo.bar

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.time.Duration.Companion.seconds

fun main() {
  runBlocking {
    try {
      withContext(Dispatchers.Default) {
        delay(1.seconds)
        doWork()
      }
    }
    catch (t: Throwable) {
      println("trace when caught:")
      t.printStackTrace(System.out)
    }
  }
}

private fun doWork(): Int {
  val ise = IllegalStateException("Internal invariant failed")
  println("trace when thrown:")
  ise.printStackTrace(System.out)
  throw ise
}
trace when thrown:
java.lang.IllegalStateException: Internal invariant failed
	at com.foo.bar.RecoveryExampleKt.doWork(recoveryExample.kt:26)
	at com.foo.bar.RecoveryExampleKt.access$doWork(recoveryExample.kt:1)
	at com.foo.bar.RecoveryExampleKt$main$1$1.invokeSuspend(recoveryExample.kt:15)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
trace when caught:
java.lang.IllegalStateException: Internal invariant failed
	at com.foo.bar.RecoveryExampleKt.doWork(recoveryExample.kt:26)
	at com.foo.bar.RecoveryExampleKt.access$doWork(recoveryExample.kt:1)
	at com.foo.bar.RecoveryExampleKt$main$1$1.invokeSuspend(recoveryExample.kt:15)
	at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:46)
	at com.foo.bar.RecoveryExampleKt$main$1.invokeSuspend(recoveryExample.kt:13)
Caused by: java.lang.IllegalStateException: Internal invariant failed
	at com.foo.bar.RecoveryExampleKt.doWork(recoveryExample.kt:26)
	at com.foo.bar.RecoveryExampleKt.access$doWork(recoveryExample.kt:1)
	at com.foo.bar.RecoveryExampleKt$main$1$1.invokeSuspend(recoveryExample.kt:15)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)

dovchinnikov avatar Feb 15 '24 15:02 dovchinnikov