Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/JobSupport.kt (Kotlin)

Code before:
) : JobNode<JobSupport>(job) {
    private val _invoked = atomic(false)

    override fun invoke(cause: Throwable?) {
        if (_invoked.compareAndSet(false, true)) {
            val state = job.state
            assert { state !is Incomplete }
            if (state is CompletedExceptionally) {
                // Resume with with the corresponding exception to preserve it
                continuation.resumeWithException(state.cause)
            } else {
                // Resuming with value in a cancellable way (AwaitContinuation is configured for this mode).
                @Suppress("UNCHECKED_CAST")
                continuation.resume(state.unboxState() as T)
            }
        }
    }

    override fun toString() = "ResumeAwaitOnCompletion[$continuation]"
}

internal class DisposeOnCompletion(
    job: Job,
    private val handle: DisposableHandle
) : JobNode<Job>(job) {
    private val _invoked = atomic(false)

    override fun invoke(cause: Throwable?) {
        if (_invoked.compareAndSet(false, true)) {
            handle.dispose()
        }
    }

    override fun toString(): String = "DisposeOnCompletion[$handle]"
}

private class SelectJoinOnCompletion<R>(
    job: JobSupport,
    private val select: SelectInstance<R>,
    private val block: suspend () -> R
) : JobNode<JobSupport>(job) {
    private val _invoked = atomic(false)

    override fun invoke(cause: Throwable?) {
        if (_invoked.compareAndSet(false, true) && select.trySelect())
            block.startCoroutineCancellable(select.completion)
    }

    override fun toString(): String = "SelectJoinOnCompletion[$select]"
}


Review comment (suggestion):
you can avoid additional field here by storing `handle` in atomic variable and CAS-ing it to `null`

Code after:
    override fun toString(): String =
        if (DEBUG) getString("Active") else super.toString()
}

internal class InactiveNodeList(
    override val list: NodeList
) : Incomplete {
    override val isActive: Boolean get() = false
    override fun toString(): String = if (DEBUG) list.getString("New") else super.toString()
}

private class InvokeOnCompletion(
    private val handler: CompletionHandler
) : JobNode() {
    override fun invokeOnce(cause: Throwable?) = handler.invoke(cause)
    override fun toString() = "InvokeOnCompletion[$classSimpleName@$hexAddress]"
}

private class ResumeOnCompletion(
    private val continuation: Continuation<Unit>
) : JobNode() {
    override fun invokeOnce(cause: Throwable?) = continuation.resume(Unit)
    override fun toString() = "ResumeOnCompletion[$continuation]"
}

private class ResumeAwaitOnCompletion<T>(
    private val continuation: CancellableContinuationImpl<T>
) : JobNode() {
    override fun invokeOnce(cause: Throwable?) {
        val state = job.state
        assert { state !is Incomplete }
        if (state is CompletedExceptionally) {
            // Resume with with the corresponding exception to preserve it
            continuation.resumeWithException(state.cause)
        } else {
            // Resuming with value in a cancellable way (AwaitContinuation is configured for this mode).
            @Suppress("UNCHECKED_CAST")
            continuation.resume(state.unboxState() as T)
        }
    }
    override fun toString() = "ResumeAwaitOnCompletion[$continuation]"
}

internal class DisposeOnCompletion(
    private val handle: DisposableHandle
) : JobNode() {
    override fun invokeOnce(cause: Throwable?) = handle.dispose()
    override fun toString(): String = "DisposeOnCompletion[${handle}]"
}

private class SelectJoinOnCompletion<R>(