works-at-coroutine-leve

Works at co-routine level.

One thing that needs to be emphasized here is that we suspend a coroutine, not a function. Suspending functions are not coroutines, just functions that can suspend a coroutine12. Imagine that we store a function in some variable and try to resume it after the function call. - Kotlin Coroutines Deep Dive

GT Snapshot: resume will never be called as co-routine was suspended.

Code

package com.glassthought.sandbox

import gt.sandbox.util.output.Out
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

val out = Out.standard()

// Do not do this
var continuation: Continuation<Unit>? = null
suspend fun suspendAndSetContinuation() {

  out.info("Suspending coroutine")
  suspendCoroutine<Unit> { cont ->
    continuation = cont
  }
}

suspend fun main() {
  out.info("Before")
  suspendAndSetContinuation()

  // We will never get here as the co-routine was suspended without
  // ever being resumed
  continuation?.resume(Unit)
  out.info ("After")
}

Command to reproduce:

gt.sandbox.checkout.commit e595cb2a99f15c984c8c \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "_gt_no_op_for_snapshotting_with_printout"

Recorded output of command:

No command ran. Just snapshot.