Skip to content

Commit 418bddd

Browse files
committed
Make sure that exception recovery does not break exception message
Fixes #1631
1 parent 1b378ba commit 418bddd

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

kotlinx-coroutines-core/jvm/src/internal/StackTraceRecovery.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ private fun <E : Throwable> recoverFromStackFrame(exception: E, continuation: Co
6666

6767
// Try to create an exception of the same type and get stacktrace from continuation
6868
val newException = tryCopyException(cause) ?: return exception
69+
// Verify that the new exception has the same message as the original one (bail out if not, see #1631)
70+
if (newException.message != cause.message) return exception
71+
// Update stacktrace
6972
val stacktrace = createStackTrace(continuation)
7073
if (stacktrace.isEmpty()) return exception
71-
7274
// Merge if necessary
7375
if (cause !== exception) {
7476
mergeRecoveredTraces(recoveredStacktrace, stacktrace)
7577
}
76-
7778
// Take recovered stacktrace, merge it with existing one if necessary and return
7879
return createFinalException(cause, newException, stacktrace)
7980
}

kotlinx-coroutines-core/jvm/test/exceptions/StackTraceRecoveryTest.kt

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.coroutines.*
88
import kotlinx.coroutines.channels.*
99
import kotlinx.coroutines.intrinsics.*
1010
import org.junit.Test
11+
import java.lang.RuntimeException
1112
import java.util.concurrent.*
1213
import kotlin.concurrent.*
1314
import kotlin.coroutines.*
@@ -264,4 +265,18 @@ class StackTraceRecoveryTest : TestBase() {
264265
}
265266
yield() // nop to make sure it is not a tail call
266267
}
268+
269+
@Test
270+
fun testWrongMessageException() = runTest {
271+
val result = runCatching {
272+
coroutineScope<Unit> {
273+
throw WrongMessageException("OK")
274+
}
275+
}
276+
val ex = result.exceptionOrNull() ?: error("Expected to fail")
277+
assertTrue(ex is WrongMessageException)
278+
assertEquals("Token OK", ex.message)
279+
}
280+
281+
public class WrongMessageException(token: String) : RuntimeException("Token $token")
267282
}

0 commit comments

Comments
 (0)