produce throwing exception when channel is already closed

produce throwing exception when channel is already closed, can create "uncaught exception"

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*

fun main() {
    GlobalScope.produce<Int>(CoroutineExceptionHandler { _, ex -> println("Got an exception: $ex") }) {
        close(IllegalArgumentException())
        throw IllegalStateException()
    }
    Thread.sleep(100)
}

Usually, produce propagates the exception thrown in its block by closing its channel with it, but here, the channel is already closed with another exception, so we get

Got an exception: java.lang.IllegalStateException It's an uncaught exception. - dkhalanskyjb/github-convo


Backlinks