Skip to content

Commit af95e9f

Browse files
committed
~ failing test on cancellation, and a fix
1 parent fac5ec4 commit af95e9f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,17 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
508508
}
509509
}
510510

511-
private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode<*> {
512-
return if (onCancelling) {
513-
handler as? JobCancellingNode<*> ?: InvokeOnCancelling(this, handler)
511+
private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode<*> =
512+
if (onCancelling) {
513+
(handler as? JobCancellingNode<*>)
514+
?.takeIf { it.job === this }
515+
?: InvokeOnCancelling(this, handler)
514516
} else {
515-
(handler as? JobNode<*>)?.also { assert { it !is JobCancellingNode } }
517+
(handler as? JobNode<*>)
518+
?.also { assert { it !is JobCancellingNode } }
519+
?.takeIf { it.job === this }
516520
?: InvokeOnCompletion(this, handler)
517521
}
518-
}
519522

520523
private fun addLastAtomic(expect: Any, list: NodeList, node: JobNode<*>) =
521524
list.addLastIf(node) { this.state === expect }

kotlinx-coroutines-core/common/test/AwaitTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,18 @@ class AwaitTest : TestBase() {
365365
awaitAll(delegate)
366366
finish(4)
367367
}
368+
369+
@Test
370+
fun testCancelAwaitAllDelegate() = runTest {
371+
expect(1)
372+
val deferred = CompletableDeferred<String>()
373+
val delegate = object : Deferred<String> by deferred {}
374+
launch {
375+
expect(3)
376+
deferred.cancel()
377+
}
378+
expect(2)
379+
assertFailsWith<CancellationException> { awaitAll(delegate) }
380+
finish(4)
381+
}
368382
}

0 commit comments

Comments
 (0)