diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt b/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt index cad34a0d55..2a701c0c12 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Delay.kt @@ -394,6 +394,7 @@ private fun Flow.timeoutInternal( value.onSuccess { downStream.emit(it) }.onClosed { + it?.let { throw it } return@onReceiveCatching false } return@onReceiveCatching true diff --git a/kotlinx-coroutines-core/common/test/flow/operators/TimeoutTest.kt b/kotlinx-coroutines-core/common/test/flow/operators/TimeoutTest.kt index a2ca101ef0..0162a216c3 100644 --- a/kotlinx-coroutines-core/common/test/flow/operators/TimeoutTest.kt +++ b/kotlinx-coroutines-core/common/test/flow/operators/TimeoutTest.kt @@ -237,6 +237,17 @@ class TimeoutTest : TestBase() { testImmediateTimeout(-1.seconds) } + @Test + fun testClosing() = runTest { + assertFailsWith { + channelFlow { close(TestException()) } + .timeout(Duration.INFINITE) + .collect { + expectUnreached() + } + } + } + private fun testImmediateTimeout(timeout: Duration) { expect(1) val flow = emptyFlow().timeout(timeout)