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

Fixes #1966
  • Loading branch information
qwwdfsad committed Apr 28, 2020
1 parent eb4e7d3 commit 250b04f
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 @@ -119,4 +119,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 250b04f

Please sign in to comment.