in kotlinx-coroutines-core/common/src/channels/BufferedChannel.kt [1741:1762]
fun tryResumeHasNextOnClosedChannel() {
/*
* Read the current continuation of the suspended `hasNext()` call and clean the corresponding field to avoid memory leaks.
* While this nulling out is unnecessary, it eliminates memory leaks (through the continuation)
* if the channel iterator accidentally remains GC-reachable after the channel is closed.
*/
val cont = this.continuation!!
this.continuation = null
// Update the `hasNext()` internal result and inform
// `BufferedChannel` extensions that synchronization
// of this receive operation is completed.
this.receiveResult = CHANNEL_CLOSED
// If this channel was closed without exception,
// `hasNext()` should return `false`; otherwise,
// it throws the closing exception.
val cause = closeCause
if (cause == null) {
cont.resume(false)
} else {
cont.resumeWithException(recoverStackTrace(cause, cont))
}
}