Skip to content

Change order of handlers in JobSupport and CancellableContinuation #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ internal abstract class AbstractContinuation<in T>(
return updateStateToFinal(state, CancelledContinuation(this, cause), mode = MODE_ATOMIC_DEFAULT)
}

private fun onCompletionInternal(mode: Int) {
private fun dispatchResume(mode: Int) {
if (tryResume()) return // completed before getResult invocation -- bail out
// otherwise, getResult has already commenced, i.e. completed later or in other thread
dispatch(mode)
Expand Down Expand Up @@ -326,16 +326,18 @@ internal abstract class AbstractContinuation<in T>(

protected fun completeStateUpdate(expect: NotCompleted, update: Any?, mode: Int) {
val exceptionally = update as? CompletedExceptionally
onCompletionInternal(mode)

// Invoke cancellation handlers only if necessary
if (update is CancelledContinuation && expect is CancelHandler) {
try {
expect.invoke(exceptionally?.cause)
} catch (ex: Throwable) {
handleException(CompletionHandlerException("Exception in completion handler $expect for $this", ex))
}
}

// Notify all handlers before dispatching, otherwise behaviour will be timing-dependent
// and confusing with Unconfined
dispatchResume(mode)
}

private fun handleException(exception: Throwable) {
Expand Down
15 changes: 13 additions & 2 deletions common/kotlinx-coroutines-core-common/src/JobSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,17 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
private fun completeUpdateState(expect: Incomplete, update: Any?, mode: Int) {
val exceptionally = update as? CompletedExceptionally
// Do overridable processing before completion handlers
if (!expect.isCancelling) onCancellationInternal(exceptionally) // only notify when was not cancelling before
onCompletionInternal(update, mode)

/*
* 1) Invoke onCancellationInternal: exception handling, parent/resource cancellation etc.
* 2) Invoke completion handlers: .join(), callbacks etc. It's important to invoke them only AFTER exception handling, see #208
* 3) Invoke onCompletionInternal: onNext(), timeout deregistration etc. I should be last so all callbacks observe consistent state
* of the job which doesn't depend on callback scheduling
*
* Only notify on cancellation once (expect.isCancelling)
*/
if (!expect.isCancelling) onCancellationInternal(exceptionally)

// Invoke completion handlers
val cause = exceptionally?.cause
if (expect is JobNode<*>) { // SINGLE/SINGLE+ state -- one completion handler (common case)
Expand All @@ -220,6 +229,8 @@ internal open class JobSupport constructor(active: Boolean) : Job, SelectClause0
} else {
expect.list?.notifyCompletion(cause)
}

onCompletionInternal(update, mode)
}

private inline fun <reified T: JobNode<*>> notifyHandlers(list: NodeList, cause: Throwable?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ class AbstractCoroutineTest : TestBase() {

override fun onCompleted(value: String) {
assertEquals("OK", value)
expect(6)
expect(8)
}

override fun onCompletedExceptionally(exception: Throwable) {
expectUnreached()
}
}

coroutine.invokeOnCompletion(onCancelling = true) {
assertTrue(it == null)
expect(7)
expect(6)
}

coroutine.invokeOnCompletion {
assertTrue(it == null)
expect(8)
expect(7)
}
expect(2)
coroutine.start()
Expand Down Expand Up @@ -66,7 +68,7 @@ class AbstractCoroutineTest : TestBase() {

override fun onCompletedExceptionally(exception: Throwable) {
assertTrue(exception is TestException1)
expect(8)
expect(9)
}
}
coroutine.invokeOnCompletion(onCancelling = true) {
Expand All @@ -75,7 +77,7 @@ class AbstractCoroutineTest : TestBase() {
}
coroutine.invokeOnCompletion {
assertTrue(it is TestException1)
expect(9)
expect(8)
}
expect(2)
coroutine.start()
Expand Down
30 changes: 30 additions & 0 deletions common/kotlinx-coroutines-core-common/test/channels/ProduceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.coroutines.experimental.*
import kotlin.test.*

class ProduceTest : TestBase() {

@Test
fun testBasic() = runTest {
val c = produce(coroutineContext) {
Expand Down Expand Up @@ -81,5 +82,34 @@ class ProduceTest : TestBase() {
}
}

@Test
fun testCancelOnCompletionUnconfined() = runTest {
cancelOnCompletion(Unconfined)
}

@Test
fun testCancelOnCompletion() = runTest {
cancelOnCompletion(coroutineContext)
}

private suspend fun cancelOnCompletion(coroutineContext: CoroutineContext) {
val source = Channel<Int>()
expect(1)
val produced = produce<Int>(coroutineContext, onCompletion = source.consumes()) {
expect(2)
source.receive()
}

yield()
expect(3)
produced.cancel()
try {
source.receive()
// TODO shouldn't it be ClosedReceiveChannelException ?
} catch (e: JobCancellationException) {
finish(4)
}
}

private class TestException : Exception()
}