Skip to content

Commit 6e3faa7

Browse files
elizarovqwwdfsad
authored andcommitted
Fixed spurious exception during select.onJoin clause registration
* Removed #1130 workaround from debounce * Fixed race in debounce Fixes #1130
1 parent 5ea1bed commit 6e3faa7

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

kotlinx-coroutines-core/common/src/JobSupport.kt

-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
529529
if (state !is Incomplete) {
530530
// already complete -- select result
531531
if (select.trySelect(null)) {
532-
select.completion.context.checkCompletion() // always check for our completion
533532
block.startCoroutineUnintercepted(select.completion)
534533
}
535534
return

kotlinx-coroutines-core/common/src/flow/operators/Delay.kt

+3-8
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,8 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
6464
coroutineScope {
6565
val values = Channel<Any?>(Channel.CONFLATED) // Actually Any, KT-30796
6666
// Channel is not closed deliberately as there is no close with value
67-
val collector = launch {
68-
try {
69-
collect { value -> values.send(value ?: NullSurrogate) }
70-
} catch (e: Throwable) {
71-
values.close(e) // Workaround for #1130
72-
throw e
73-
}
67+
val collector = async {
68+
collect { value -> values.send(value ?: NullSurrogate) }
7469
}
7570

7671
var isDone = false
@@ -89,7 +84,7 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> {
8984
}
9085

9186
// Close with value 'idiom'
92-
collector.onJoin {
87+
collector.onAwait {
9388
if (lastValue != null) emit(NullSurrogate.unbox(lastValue))
9489
isDone = true
9590
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:Suppress("NAMED_ARGUMENTS_NOT_ALLOWED") // KT-21913
6+
7+
package kotlinx.coroutines.selects
8+
9+
import kotlinx.coroutines.*
10+
import kotlinx.coroutines.channels.*
11+
import kotlin.test.*
12+
13+
class SelectLoopTest : TestBase() {
14+
// https://github.com/Kotlin/kotlinx.coroutines/issues/1130
15+
@Test
16+
fun testChannelSelectLoop() = runTest(
17+
expected = { it is TestException }
18+
) {
19+
expect(1)
20+
val channel = Channel<Unit>()
21+
val job = launch {
22+
expect(2)
23+
channel.send(Unit)
24+
expect(3)
25+
throw TestException()
26+
}
27+
var isDone = false
28+
while (!isDone) {
29+
select<Unit> {
30+
channel.onReceiveOrNull {
31+
expect(4)
32+
assertEquals(Unit, it)
33+
}
34+
job.onJoin {
35+
expect(5)
36+
isDone = true
37+
}
38+
}
39+
}
40+
finish(6)
41+
}
42+
}

0 commit comments

Comments
 (0)