cancel

abstract fun cancel(): Boolean (source)

Cancels reception of remaining elements from this channel. This function closes the channel and removes all buffered sent elements from it. This function returns true if the channel was not closed previously, or false otherwise.

Immediately after invocation of this function isClosedForReceive and isClosedForSend on the side of SendChannel start returning true, so all attempts to send to this channel afterwards will throw ClosedSendChannelException, while attempts to receive will throw ClosedReceiveChannelException.

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

Cancels reception of remaining elements from this channel. This function closes the channel with the specified cause (unless it was already closed) and removes all buffered sent elements from it. This function returns true if the channel was not closed previously, or false otherwise.

Immediately after invocation of this function isClosedForReceive and isClosedForSend on the side of SendChannel start returning true, so all attempts to send to this channel afterwards will throw ClosedSendChannelException, while attempts to receive will throw ClosedReceiveChannelException if it was cancelled without a cause. A channel that was cancelled with non-null cause is called a failed channel. Attempts to send or receive on a failed channel throw the specified cause exception.

Note: This is an experimental api. Semantics of cancelling a channel with exception may change in the future or this feature may be completely removed.