Skip to content

Commit 5610e1d

Browse files
committed
Bug fix: start hangs on lazy coroutine with attached invokeOnCompletion
1 parent 75675e6 commit 5610e1d

File tree

2 files changed

+22
-4
lines changed
  • core/kotlinx-coroutines-core/src

2 files changed

+22
-4
lines changed

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Job.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,8 +1128,8 @@ public open class JobSupport(active: Boolean) : Job, SelectClause0, SelectClause
11281128

11291129
fun tryMakeActive(): Int {
11301130
if (_active.value != 0) return FALSE
1131-
if (_active.compareAndSet(0, 1)) return RETRY
1132-
return TRUE
1131+
if (_active.compareAndSet(0, 1)) return TRUE
1132+
return RETRY
11331133
}
11341134

11351135
override fun toString(): String = buildString {

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/LaunchLazyTest.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.junit.Test
2020

2121
class LaunchLazyTest : TestBase() {
2222
@Test
23-
fun testLaunchAndYieldJoin() = runBlocking {
23+
fun testLaunchAndYieldJoin() = runTest {
2424
expect(1)
2525
val job = launch(coroutineContext, CoroutineStart.LAZY) {
2626
expect(4)
@@ -37,7 +37,7 @@ class LaunchLazyTest : TestBase() {
3737
}
3838

3939
@Test
40-
fun testStart() = runBlocking {
40+
fun testStart() = runTest {
4141
expect(1)
4242
val job = launch(coroutineContext, CoroutineStart.LAZY) {
4343
expect(5)
@@ -62,4 +62,22 @@ class LaunchLazyTest : TestBase() {
6262
job.join() // immediately returns
6363
finish(9)
6464
}
65+
66+
@Test
67+
fun testInvokeOnCompletionAndStart() = runTest {
68+
expect(1)
69+
val job = launch(coroutineContext, CoroutineStart.LAZY) {
70+
expect(5)
71+
}
72+
yield() // no started yet!
73+
expect(2)
74+
job.invokeOnCompletion {
75+
expect(6)
76+
}
77+
expect(3)
78+
job.start()
79+
expect(4)
80+
yield()
81+
finish(7)
82+
}
6583
}

0 commit comments

Comments
 (0)