Skip to content

Commit 6d24aab

Browse files
qwwdfsadelizarov
authored andcommitted
Materialize an exception for Job onCancelling handlers
Fixes #436
1 parent 732474f commit 6d24aab

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import kotlinx.coroutines.experimental.internalAnnotations.*
1010
import kotlinx.coroutines.experimental.intrinsics.*
1111
import kotlinx.coroutines.experimental.selects.*
1212
import kotlin.coroutines.experimental.*
13-
import kotlin.coroutines.experimental.intrinsics.*
1413

1514
/**
1615
* A concrete implementation of [Job]. It is optionally a child to a parent job.
@@ -524,7 +523,8 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
524523
if (!_state.compareAndSet(expect, Finishing(list, cancelled, false))) return false
525524
onFinishingInternal(cancelled)
526525
onCancellationInternal(cancelled)
527-
notifyCancellation(list, cause)
526+
// Materialize cause
527+
notifyCancellation(list, cancelled.cause)
528528
return true
529529
}
530530

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,22 @@ class JobTest : TestBase() {
157157
parent.join()
158158
finish(6)
159159
}
160-
}
160+
161+
@Test
162+
fun testOnCancellingHandler() = runTest {
163+
val job = launch(coroutineContext) {
164+
expect(2)
165+
delay(Long.MAX_VALUE)
166+
}
167+
168+
job.invokeOnCompletion(onCancelling = true) {
169+
assertNotNull(it)
170+
expect(3)
171+
}
172+
173+
expect(1)
174+
yield()
175+
job.cancelAndJoin()
176+
finish(4)
177+
}
178+
}

0 commit comments

Comments
 (0)