Skip to content

Mutation of exception that thrown from another scope #4527

@ihostage

Description

@ihostage

Describe the bug

Handling of exception in my app is breaked by Debug mode (JVM -ea option). Is it expected (by design) and I need to be ready of this mutation of thrown exceptions or is it a bug?

Provide a Reproducer

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    try {
        throwEx()
    } catch (e: Exception) {
        processException(e)
    }
    try {
        throwExFromNewScope()
    } catch (e: Exception) {
        processException(e)
    }
}

private fun processException(e: Throwable) {
    if (e is IllegalStateException && e.cause is IllegalArgumentException) {
        println("Got correctly exception: IllegalStateException <- IllegalArgumentException")
    } else {
        e.printStackTrace()
    }
}

private suspend fun throwExFromNewScope() = coroutineScope {
    throwEx()
}

private suspend fun throwEx() {
    throw IllegalStateException(IllegalArgumentException("error"))
}

The code above works ok without Debug mode:

Got correctly exception: IllegalStateException <- IllegalArgumentException
Got correctly exception: IllegalStateException <- IllegalArgumentException

and throw an exception with -ea option

Got correctly exception: IllegalStateException <- IllegalArgumentException
java.lang.IllegalStateException: java.lang.IllegalArgumentException: error
	at WebFaultReproducer.throwEx(WebFaultReproducer.kt:32)
	at WebFaultReproducer.access$throwEx(WebFaultReproducer.kt:1)
	at WebFaultReproducer$throwExFromNewScope$2.invokeSuspend(WebFaultReproducer.kt:28)
	at WebFaultReproducer$throwExFromNewScope$2.invoke(WebFaultReproducer.kt)
	at WebFaultReproducer$throwExFromNewScope$2.invoke(WebFaultReproducer.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndspatched(Undispatched.kt:66)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:43)
	at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:286)
	at WebFaultReproducer.throwExFromNewScope(WebFaultReproducer.kt:27)
	at WebFaultReproducer.access$throwExFromNewScope(WebFaultReproducer.kt:1)
	at WebFaultReproducer$main$1.invokeSuspend(WebFaultReproducer.kt:13)
	at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
	at WebFaultReproducer$main$1.invokeSuspend(WebFaultReproducer.kt:13)
Caused by: java.lang.IllegalStateException: java.lang.IllegalArgumentException: error
	at WebFaultReproducer.throwEx(WebFaultReproducer.kt:32)
	at WebFaultReproducer.access$throwEx(WebFaultReproducer.kt:1)
	at WebFaultReproducer$throwExFromNewScope$2.invokeSuspend(WebFaultReproducer.kt:28)
	at WebFaultReproducer$throwExFromNewScope$2.invoke(WebFaultReproducer.kt)
	at WebFaultReproducer$throwExFromNewScope$2.invoke(WebFaultReproducer.kt)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndspatched(Undispatched.kt:66)
	at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:43)
	at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:286)
	at WebFaultReproducer.throwExFromNewScope(WebFaultReproducer.kt:27)
	at WebFaultReproducer.access$throwExFromNewScope(WebFaultReproducer.kt:1)
	at WebFaultReproducer$main$1.invokeSuspend(WebFaultReproducer.kt:13)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:34)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:263)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:94)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:70)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:48)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at WebFaultReproducer.main(WebFaultReproducer.kt:6)
	at WebFaultReproducer.main(WebFaultReproducer.kt)
Caused by: java.lang.IllegalArgumentException: error
	... 21 more

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions