Skip to content

Commit

Permalink
Revisit all @transient usages in coroutines (#4294)
Browse files Browse the repository at this point in the history
Fixes #4293
  • Loading branch information
qwwdfsad authored Dec 11, 2024
1 parent 6b73546 commit 9f6c80b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion kotlinx-coroutines-core/jvm/src/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ public actual fun CancellationException(message: String?, cause: Throwable?) : C
internal actual class JobCancellationException public actual constructor(
message: String,
cause: Throwable?,
@JvmField @Transient internal actual val job: Job
job: Job
) : CancellationException(message), CopyableThrowable<JobCancellationException> {

@Transient
private val _job: Job? = job

// The safest option for transient -- return something that meanigfully reject any attemp to interact with the job
internal actual val job get() = _job ?: NonCancellable

init {
if (cause != null) initCause(cause)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package kotlinx.coroutines.flow.internal

import kotlinx.coroutines.*

/**
* Implementation note: `owner` is an internal marked that is used ONLY for identity checks by coroutines machinery,
* and it's never exposed, thus it's safe to have it both `@Transient` and non-nullable.
*/
internal actual class AbortFlowException actual constructor(
@JvmField @Transient actual val owner: Any
) : CancellationException("Flow was aborted, no more elements needed") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ internal actual fun propagateExceptionFinalResort(exception: Throwable) {
}

// This implementation doesn't store a stacktrace, which is good because a stacktrace doesn't make sense for this.
internal actual class DiagnosticCoroutineContextException actual constructor(@Transient private val context: CoroutineContext) : RuntimeException() {
internal actual class DiagnosticCoroutineContextException actual constructor(context: CoroutineContext) : RuntimeException() {

@Transient
private val context: CoroutineContext? = context

override fun getLocalizedMessage(): String {
return context.toString()
}
Expand Down

0 comments on commit 9f6c80b

Please sign in to comment.