diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt index 503592fc6..61cea61ce 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/subscribeMessages.kt @@ -11,6 +11,7 @@ package net.mamoe.mirai.event +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel @@ -172,9 +173,12 @@ inline fun CoroutineScope.incoming( capacity: Int = Channel.UNLIMITED ): ReceiveChannel { return Channel(capacity).apply { - subscribeAlways(coroutineContext) { + val listener = subscribeAlways(coroutineContext) { send(this) } + this.invokeOnClose { + listener.cancel(CancellationException("ReceiveChannel closed", it)) + } } }