Skip to content

Commit 9f6c80b

Browse files
authored
Revisit all @transient usages in coroutines (#4294)
Fixes #4293
1 parent 6b73546 commit 9f6c80b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ public actual fun CancellationException(message: String?, cause: Throwable?) : C
2424
internal actual class JobCancellationException public actual constructor(
2525
message: String,
2626
cause: Throwable?,
27-
@JvmField @Transient internal actual val job: Job
27+
job: Job
2828
) : CancellationException(message), CopyableThrowable<JobCancellationException> {
2929

30+
@Transient
31+
private val _job: Job? = job
32+
33+
// The safest option for transient -- return something that meanigfully reject any attemp to interact with the job
34+
internal actual val job get() = _job ?: NonCancellable
35+
3036
init {
3137
if (cause != null) initCause(cause)
3238
}

kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package kotlinx.coroutines.flow.internal
22

33
import kotlinx.coroutines.*
44

5+
/**
6+
* Implementation note: `owner` is an internal marked that is used ONLY for identity checks by coroutines machinery,
7+
* and it's never exposed, thus it's safe to have it both `@Transient` and non-nullable.
8+
*/
59
internal actual class AbortFlowException actual constructor(
610
@JvmField @Transient actual val owner: Any
711
) : CancellationException("Flow was aborted, no more elements needed") {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ internal actual fun propagateExceptionFinalResort(exception: Throwable) {
3232
}
3333

3434
// This implementation doesn't store a stacktrace, which is good because a stacktrace doesn't make sense for this.
35-
internal actual class DiagnosticCoroutineContextException actual constructor(@Transient private val context: CoroutineContext) : RuntimeException() {
35+
internal actual class DiagnosticCoroutineContextException actual constructor(context: CoroutineContext) : RuntimeException() {
36+
37+
@Transient
38+
private val context: CoroutineContext? = context
39+
3640
override fun getLocalizedMessage(): String {
3741
return context.toString()
3842
}

0 commit comments

Comments
 (0)