Skip to content

Commit

Permalink
Fixed race between creation of a child & cancellation of parent job
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov committed Sep 20, 2018
1 parent 2999129 commit ee87120
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/kotlinx-coroutines-core-common/src/JobSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0

// fast-path method to finalize normally completed coroutines without children
private fun tryFinalizeSimpleState(state: Incomplete, update: Any?, mode: Int): Boolean {
check(state !is Finishing) // only for non-finishing state
check(state is Empty || state is JobNode<*>) // only simple state without lists where children can concurrently add
check(update !is CompletedExceptionally) // only for normal completion
if (!_state.compareAndSet(state, update)) return false
completeStateFinalization(state, update, mode, false)
Expand Down Expand Up @@ -735,9 +735,9 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
return COMPLETING_ALREADY_COMPLETING
// find first child
val child = firstChild(state)
// FAST PATH -- no children to wait for && not finishing && not failing => can complete immediately
// FAST PATH -- no children to wait for && simple state (no list) && not failing => can complete immediately
// Failures always have to go through Finishing state to serialize exception handling
if (child == null && state !is Finishing && proposedUpdate !is CompletedExceptionally) {
if (child == null && (state is Empty || state is JobNode<*>) && proposedUpdate !is CompletedExceptionally) {
if (!tryFinalizeSimpleState(state, proposedUpdate, mode)) return COMPLETING_RETRY
return COMPLETING_COMPLETED
}
Expand Down
62 changes: 62 additions & 0 deletions core/kotlinx-coroutines-core/test/JobChildStressTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.experimental

import org.junit.*
import org.junit.Test
import java.util.concurrent.*
import kotlin.test.*

class JobChildStressTest : TestBase() {
private val N_ITERATIONS = 10_000 * stressTestMultiplier
private val pool = newFixedThreadPoolContext(3, "JobChildStressTest")

@After
fun tearDown() {
pool.close()
}

/**
* Perform concurrent launch of a child job & cancellation of the explicit parent job
*/
@Test
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
fun testChild() = runTest {
val barrier = CyclicBarrier(3)
repeat(N_ITERATIONS) {
var wasLaunched = false
var unhandledException: Throwable? = null
val handler = CoroutineExceptionHandler { _, ex ->
unhandledException = ex
}
val scope = CoroutineScope(pool + handler)
val parent = CompletableDeferred<Unit>()
// concurrent child launcher
val launcher = scope.launch {
barrier.await()
// A: launch child for a parent job
launch(parent) {
wasLaunched = true
throw TestException()
}
}
// concurrent cancel
val canceller = scope.launch {
barrier.await()
// B: cancel parent job of a child
parent.cancel()
}
barrier.await()
joinAll(launcher, canceller, parent)
assertNull(unhandledException)
if (wasLaunched) {
val exception = parent.getCompletionExceptionOrNull()
assertTrue(exception is TestException, "exception=$exception")
}
}
}

private class TestException : Exception()
}

0 comments on commit ee87120

Please sign in to comment.