Skip to content

Commit 2113d1c

Browse files
uchuhimoelizarov
authored andcommitted
Fix error that fail to dispose handler when job is in SINGLE/SINGLE+ state
1 parent 656bcd9 commit 2113d1c

File tree

2 files changed

+23
-2
lines changed
  • kotlinx-coroutines-core/src
    • main/kotlin/kotlinx/coroutines/experimental
    • test/kotlin/kotlinx/coroutines/experimental

2 files changed

+23
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public open class JobSupport(active: Boolean) : AbstractCoroutineContextElement(
642642
when (state) {
643643
// SINGE/SINGLE+ state -- one completion handler
644644
is JobNode<*> -> {
645-
if (state !== this) return // a different job node --> we were already removed
645+
if (state !== node) return // a different job node --> we were already removed
646646
// try remove and revert back to empty state
647647
if (STATE.compareAndSet(this, state, EmptyActive)) return
648648
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class JobTest : TestBase() {
127127
var fireCount = 0
128128
for (i in 0 until n) job.invokeOnCompletion { fireCount++ }.dispose()
129129
}
130-
130+
131131
@Test
132132
fun testCancelledParent() {
133133
val parent = Job()
@@ -136,4 +136,25 @@ class JobTest : TestBase() {
136136
val child = Job(parent)
137137
check(!child.isActive)
138138
}
139+
140+
@Test
141+
fun testDisposeSingleHandler() {
142+
val job = Job()
143+
var fireCount = 0
144+
val handler = job.invokeOnCompletion { fireCount++ }
145+
handler.dispose()
146+
job.cancel()
147+
assertEquals(0, fireCount)
148+
}
149+
150+
@Test
151+
fun testDisposeMultipleHandler() {
152+
val job = Job()
153+
val handlerCount = 10
154+
var fireCount = 0
155+
val handlers = Array(handlerCount) { job.invokeOnCompletion { fireCount++ } }
156+
handlers.forEach { it.dispose() }
157+
job.cancel()
158+
assertEquals(0, fireCount)
159+
}
139160
}

0 commit comments

Comments
 (0)