https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

Paulius Ruminas

12/13/2019, 1:15 PM
Hello,
Copy code
@Test
    fun example() = runBlocking {
        val a = produce {
            var i = 0
            while (isActive) {
                send(i++)
                delay(100)
            }
        }
        val b = a.broadcast(start = CoroutineStart.DEFAULT)

        launch {
            b.consumeEach {
                println("A: $it")
            }
        }
        
        b.close()

        delay(1_000)
    }
When I close b broadcast channel I get an exception:
Copy code
Exception in thread "Test worker @coroutine#3" kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed (Coroutine boundary)
Is this the expected behavior?
d

Dominaezzz

12/13/2019, 1:22 PM
Yes. The coroutine was unconditionally waiting for the channel and the only way to interrupt it is with an exception.
a

Antanas A.

12/13/2019, 1:23 PM
exception is thrown at send()
seems like producer still produces values and doesn't react to broadcast close()
p

Paulius Ruminas

12/13/2019, 1:27 PM
Copy code
val a = produce { while (isActive) send(1) }
        val b = a.broadcast(start = CoroutineStart.DEFAULT)
        b.close()
also throws an exception
d

Dominaezzz

12/13/2019, 1:31 PM
Does,
Copy code
val a = produce { while (isActive) send(1) }
a.close()
throw an exception? (Not on computer to check)
p

Paulius Ruminas

12/13/2019, 1:35 PM
produce return a ReceiveChannel. ReceiveChannel does not have a close operation. If I call a.cancel() it works. But what I really want to understand if this is a bug in coroutines because it seems like one.
21 Views