Skip to content

Commit

Permalink
Fix class cast exception during undispatched resume of cancellable co…
Browse files Browse the repository at this point in the history
…ntinuation with onCancellation block (Kotlin#1967)

Fixes Kotlin#1966
  • Loading branch information
qwwdfsad authored and recheej committed Dec 28, 2020
1 parent 037a770 commit 8e5041f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Expand Up @@ -117,7 +117,7 @@ internal fun <T> DispatchedTask<T>.resume(delegate: Continuation<T>, useMode: In
// slow-path - use delegate
val state = takeState()
val exception = getExceptionalResult(state)?.let { recoverStackTrace(it, delegate) }
val result = if (exception != null) Result.failure(exception) else Result.success(state as T)
val result = if (exception != null) Result.failure(exception) else Result.success(getSuccessfulResult<T>(state))
when (useMode) {
MODE_ATOMIC_DEFAULT -> delegate.resumeWith(result)
MODE_CANCELLABLE -> delegate.resumeCancellableWith(result)
Expand Down
12 changes: 12 additions & 0 deletions kotlinx-coroutines-core/common/test/CancellableResumeTest.kt
Expand Up @@ -138,4 +138,16 @@ class CancellableResumeTest : TestBase() {
yield() // to coroutine -- throws cancellation exception
finish(9)
}


@Test
fun testResumeUnconfined() = runTest {
val outerScope = this
withContext(Dispatchers.Unconfined) {
val result = suspendCancellableCoroutine<String> {
outerScope.launch { it.resume("OK", {}) }
}
assertEquals("OK", result)
}
}
}

0 comments on commit 8e5041f

Please sign in to comment.