Skip to content

Commit be85455

Browse files
authored
Fix class cast exception during undispatched resume of cancellable continuation with onCancellation block (#1967)
Fixes #1966
1 parent d5766f3 commit be85455

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ internal fun <T> DispatchedTask<T>.resume(delegate: Continuation<T>, useMode: In
117117
// slow-path - use delegate
118118
val state = takeState()
119119
val exception = getExceptionalResult(state)?.let { recoverStackTrace(it, delegate) }
120-
val result = if (exception != null) Result.failure(exception) else Result.success(state as T)
120+
val result = if (exception != null) Result.failure(exception) else Result.success(getSuccessfulResult<T>(state))
121121
when (useMode) {
122122
MODE_ATOMIC_DEFAULT -> delegate.resumeWith(result)
123123
MODE_CANCELLABLE -> delegate.resumeCancellableWith(result)

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

+12
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@ class CancellableResumeTest : TestBase() {
138138
yield() // to coroutine -- throws cancellation exception
139139
finish(9)
140140
}
141+
142+
143+
@Test
144+
fun testResumeUnconfined() = runTest {
145+
val outerScope = this
146+
withContext(Dispatchers.Unconfined) {
147+
val result = suspendCancellableCoroutine<String> {
148+
outerScope.launch { it.resume("OK", {}) }
149+
}
150+
assertEquals("OK", result)
151+
}
152+
}
141153
}

0 commit comments

Comments
 (0)