Skip to content

Commit fa9586b

Browse files
committed
Fixed sporadic test crashes related to withTimeout and children
- The bug only manifested when withTimeout coroutine had children working in a different dispatcher - A separate stress-test that reproduces this problem with much higher probability
1 parent 687e482 commit fa9586b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
707707
if (state !is Finishing) onFinishingInternal(proposedUpdate)
708708
if (child != null && tryWaitForChild(child, proposedUpdate))
709709
return COMPLETING_WAITING_CHILDREN
710-
if (tryFinalizeState(completing, proposedUpdate, mode = MODE_ATOMIC_DEFAULT))
710+
if (tryFinalizeState(completing, proposedUpdate, mode))
711711
return COMPLETING_COMPLETED
712712
}
713713
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.experimental
6+
7+
import org.junit.Test
8+
import kotlin.test.*
9+
10+
class WithTimeoutChildDispatchStressTest : TestBase() {
11+
private val N_REPEATS = 10_000 * stressTestMultiplier
12+
13+
/**
14+
* This stress-test makes sure that dispatching resumption from within withTimeout
15+
* works appropriately (without additional dispatch) despite the presence of
16+
* children coroutine in a different dispatcher.
17+
*/
18+
@Test
19+
fun testChildDispatch() = runBlocking {
20+
repeat(N_REPEATS) {
21+
val result = withTimeout(5000) {
22+
// child in different dispatcher
23+
val job = launch(Dispatchers.Default) {
24+
// done nothing, but dispatches to join from another thread
25+
}
26+
job.join()
27+
"DONE"
28+
}
29+
assertEquals("DONE", result)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)