Skip to content

Commit 9da9e35

Browse files
committed
AbstractCoroutine: onCancellation shall be invoked before installed
cancellation handler
1 parent ea4f2f3 commit 9da9e35

File tree

3 files changed

+16
-8
lines changed
  • common/kotlinx-coroutines-core-common/src/test/kotlin/kotlinx/coroutines/experimental
  • core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental
  • js/kotlinx-coroutines-core-js/src/main/kotlin/kotlinx/coroutines/experimental

3 files changed

+16
-8
lines changed

common/kotlinx-coroutines-core-common/src/test/kotlin/kotlinx/coroutines/experimental/AbstractCoroutineTest.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ class AbstractCoroutineTest : TestBase() {
4141
expectUnreached()
4242
}
4343
}
44-
coroutine.invokeOnCompletion {
44+
coroutine.invokeOnCompletion(onCancelling = true) {
4545
assertTrue(it == null)
4646
expect(7)
4747
}
48+
coroutine.invokeOnCompletion {
49+
assertTrue(it == null)
50+
expect(8)
51+
}
4852
expect(2)
4953
coroutine.start()
5054
expect(4)
5155
coroutine.resume("OK")
52-
finish(8)
56+
finish(9)
5357
}
5458

5559
@Test
@@ -71,20 +75,24 @@ class AbstractCoroutineTest : TestBase() {
7175

7276
override fun onCompletedExceptionally(exception: Throwable) {
7377
assertTrue(exception is TestException1)
74-
expect(7)
78+
expect(8)
7579
}
7680
}
81+
coroutine.invokeOnCompletion(onCancelling = true) {
82+
assertTrue(it is TestException0)
83+
expect(6)
84+
}
7785
coroutine.invokeOnCompletion {
7886
assertTrue(it is TestException1)
79-
expect(8)
87+
expect(9)
8088
}
8189
expect(2)
8290
coroutine.start()
8391
expect(4)
8492
coroutine.cancel(TestException0())
85-
expect(6)
93+
expect(7)
8694
coroutine.resumeWithException(TestException1())
87-
finish(9)
95+
finish(10)
8896
}
8997

9098
private class TestException0 : Throwable()

core/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
@@ -1012,8 +1012,8 @@ internal actual open class JobSupport actual constructor(active: Boolean) : Job,
10121012
private fun tryMakeCancelling(expect: Incomplete, list: NodeList, cause: Throwable?): Boolean {
10131013
val cancelled = Cancelled(this, cause)
10141014
if (!_state.compareAndSet(expect, Finishing(list, cancelled, false))) return false
1015-
notifyCancellation(list, cause)
10161015
onCancellationInternal(cancelled)
1016+
notifyCancellation(list, cause)
10171017
return true
10181018
}
10191019

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,8 @@ internal actual open class JobSupport actual constructor(active: Boolean) : Job
724724
private fun makeCancellingList(list: NodeList, cause: Throwable?) {
725725
val cancelled = Cancelled(this, cause)
726726
state = Finishing(list, cancelled, false)
727-
notifyCancellation(list, cause)
728727
onCancellationInternal(cancelled)
728+
notifyCancellation(list, cause)
729729
}
730730

731731
private fun makeCompletingOnCancel(cause: Throwable?): Boolean =

0 commit comments

Comments
 (0)