Fixed race between creation of a child & cancellation of parent job
diff --git a/common/kotlinx-coroutines-core-common/src/JobSupport.kt b/common/kotlinx-coroutines-core-common/src/JobSupport.kt
index a8ae208..7c1ddf4 100644
--- a/common/kotlinx-coroutines-core-common/src/JobSupport.kt
+++ b/common/kotlinx-coroutines-core-common/src/JobSupport.kt
@@ -255,7 +255,7 @@
 
     // 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)
@@ -735,9 +735,9 @@
             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
         }
diff --git a/core/kotlinx-coroutines-core/test/JobChildStressTest.kt b/core/kotlinx-coroutines-core/test/JobChildStressTest.kt
new file mode 100644
index 0000000..fca7ff6
--- /dev/null
+++ b/core/kotlinx-coroutines-core/test/JobChildStressTest.kt
@@ -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()
+}
\ No newline at end of file