Skip to content

Commit 9eb807b

Browse files
committed
Mark context in DiagnosticCoroutineContextException as transient
Fixes #3328
1 parent 19666ac commit 9eb807b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

integration-testing/src/withGuavaTest/kotlin/ListAllCoroutineThrowableSubclassesTest.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package kotlinx.coroutines
77
import com.google.common.reflect.*
88
import kotlinx.coroutines.*
99
import org.junit.Test
10+
import java.io.Serializable
11+
import java.lang.reflect.Modifier
1012
import kotlin.test.*
1113

1214
class ListAllCoroutineThrowableSubclassesTest {
@@ -31,13 +33,21 @@ class ListAllCoroutineThrowableSubclassesTest {
3133
"kotlinx.coroutines.channels.ClosedReceiveChannelException",
3234
"kotlinx.coroutines.flow.internal.ChildCancelledException",
3335
"kotlinx.coroutines.flow.internal.AbortFlowException",
34-
)
36+
)
3537

3638
@Test
3739
fun testThrowableSubclassesAreSerializable() {
3840
val classes = ClassPath.from(this.javaClass.classLoader)
3941
.getTopLevelClassesRecursive("kotlinx.coroutines");
4042
val throwables = classes.filter { Throwable::class.java.isAssignableFrom(it.load()) }.map { it.toString() }
43+
for (throwable in throwables) {
44+
for (field in throwable.javaClass.declaredFields) {
45+
if (Modifier.isStatic(field.modifiers)) continue
46+
val type = field.type
47+
assertTrue(type.isPrimitive || Serializable::class.java.isAssignableFrom(type),
48+
"Throwable $throwable has non-serializable field $field")
49+
}
50+
}
4151
assertEquals(knownThrowables.sorted(), throwables.sorted())
4252
}
4353
}

kotlinx-coroutines-core/jvm/src/CoroutineExceptionHandlerImpl.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private val handlers: List<CoroutineExceptionHandler> = ServiceLoader.load(
2929
* The purpose of this exception is to add an otherwise inaccessible diagnostic information and to
3030
* be able to poke the failing coroutine context in the debugger.
3131
*/
32-
private class DiagnosticCoroutineContextException(private val context: CoroutineContext) : RuntimeException() {
32+
private class DiagnosticCoroutineContextException(@Transient private val context: CoroutineContext) : RuntimeException() {
3333
override fun getLocalizedMessage(): String {
3434
return context.toString()
3535
}

0 commit comments

Comments
 (0)