CancellableContinuation

interface CancellableContinuation<in T> : Continuation<T> (source)

Cancellable continuation. It is completed when resumed or cancelled. When the cancel function is explicitly invoked, this continuation immediately resumes with a CancellationException or the specified cancel cause.

An instance of CancellableContinuation is created by the suspendCancellableCoroutine function.

Cancellable continuation has three states (as subset of Job states):

State isActive isCompleted isCancelled
Active (initial state) true false false
Resumed (final completed state) false true false
Canceled (final completed state) false true true

Invocation of cancel transitions this continuation from active to cancelled state, while invocation of Continuation.resume or Continuation.resumeWithException transitions it from active to resumed state.

A cancelled continuation implies that it is completed.

Invocation of Continuation.resume or Continuation.resumeWithException in resumed state produces an IllegalStateException, but is ignored in cancelled state.

    +-----------+   resume    +---------+
    |  Active   | ----------> | Resumed |
    +-----------+             +---------+
          |
          | cancel
          V
    +-----------+
    | Cancelled |
    +-----------+

Properties

isActive

abstract val isActive: Boolean

Returns true when this continuation is active – it has not completed or cancelled yet.

isCancelled

abstract val isCancelled: Boolean

Returns true if this continuation was cancelled.

isCompleted

abstract val isCompleted: Boolean

Returns true when this continuation has completed for any reason. A cancelled continuation is also considered complete.

Functions

cancel

abstract fun cancel(cause: Throwable? = null): Boolean

Cancels this continuation with an optional cancellation cause. The result is true if this continuation was cancelled as a result of this invocation, and false otherwise.

invokeOnCancellation

abstract fun invokeOnCancellation(
    handler: CompletionHandler
): Unit

Registers a handler to be synchronously invoked on cancellation (regular or exceptional) of this continuation. When the continuation is already cancelled, the handler is immediately invoked with the cancellation exception. Otherwise, the handler will be invoked as soon as this continuation is cancelled.

resume

abstract fun resume(
    value: T,
    onCancellation: (cause: Throwable) -> Unit
): Unit

Resumes this continuation with the specified value and calls the specified onCancellation handler when either resumed too late (when continuation was already cancelled) or, although resumed successfully (before cancellation), the coroutine’s job was cancelled before it had a chance to run in its dispatcher, so that the suspended function threw an exception instead of returning this value.

resumeUndispatched

abstract fun CoroutineDispatcher.resumeUndispatched(
    value: T
): Unit

Resumes this continuation with the specified value in the invoker thread without going through the dispatch function of the CoroutineDispatcher in the context. This function is designed to only be used by CoroutineDispatcher implementations. It should not be used in general code.

resumeUndispatchedWithException

abstract fun CoroutineDispatcher.resumeUndispatchedWithException(
    exception: Throwable
): Unit

Resumes this continuation with the specified exception in the invoker thread without going through the dispatch function of the CoroutineDispatcher in the context. This function is designed to only be used by CoroutineDispatcher implementations. It should not be used in general code.

tryResume

abstract fun tryResume(
    value: T,
    idempotent: Any?,
    onCancellation: (cause: Throwable) -> Unit
): Any?

Same as tryResume but with onCancellation handler that called if and only if the value is not delivered to the caller because of the dispatch in the process, so that atomicity delivery guaranteed can be provided by having a cancellation fallback.

Extension Functions

cancelFutureOnCancellation

fun CancellableContinuation<*>.cancelFutureOnCancellation(
    future: Future<*>
): Unit

Cancels a specified future when this job is cancelled. This is a shortcut for the following code with slightly more efficient implementation (one fewer object created).