Skip to content

Commit fac5ec4

Browse files
committed
Remove requirement that job of the pre-created JobCancelNode have to be equal to outer job
Job is supposed to be "sealed" interface, but we also have non-sealed Deferred that can be successfully implemented via delegation and such delegation may break code (if assertions are enabled!) in a very subtle ways. This assertion is our internal invariant that we're preserving anyway, so it's worth to lift it to simplify life of our users in the future Fixes #2423
1 parent 556f07a commit fac5ec4

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

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

511511
private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode<*> {
512-
return if (onCancelling)
513-
(handler as? JobCancellingNode<*>)?.also { assert { it.job === this } }
514-
?: InvokeOnCancelling(this, handler)
515-
else
516-
(handler as? JobNode<*>)?.also { assert { it.job === this && it !is JobCancellingNode } }
512+
return if (onCancelling) {
513+
handler as? JobCancellingNode<*> ?: InvokeOnCancelling(this, handler)
514+
} else {
515+
(handler as? JobNode<*>)?.also { assert { it !is JobCancellingNode } }
517516
?: InvokeOnCompletion(this, handler)
517+
}
518518
}
519519

520520
private fun addLastAtomic(expect: Any, list: NodeList, node: JobNode<*>) =

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,18 @@ class AwaitTest : TestBase() {
351351
async(NonCancellable) { throw TestException() }
352352
joinAll(job, job, job)
353353
}
354+
355+
@Test
356+
fun testAwaitAllDelegates() = runTest {
357+
expect(1)
358+
val deferred = CompletableDeferred<String>()
359+
val delegate = object : Deferred<String> by deferred {}
360+
launch {
361+
expect(3)
362+
deferred.complete("OK")
363+
}
364+
expect(2)
365+
awaitAll(delegate)
366+
finish(4)
367+
}
354368
}

0 commit comments

Comments
 (0)