Skip to content

Commit 9587c35

Browse files
committed
fixup! Do not report exceptions raised in CoroutineDispatcher.dispatch as internal errors
Prevent reporting DispatchException itself
1 parent 08439cc commit 9587c35

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ import kotlin.coroutines.*
1616
*/
1717
@InternalCoroutinesApi
1818
public fun handleCoroutineException(context: CoroutineContext, exception: Throwable) {
19+
val reportException = if (exception is DispatchException) exception.cause!! else exception
1920
// Invoke an exception handler from the context if present
2021
try {
2122
context[CoroutineExceptionHandler]?.let {
22-
it.handleException(context, exception)
23+
it.handleException(context, reportException)
2324
return
2425
}
2526
} catch (t: Throwable) {
26-
handleUncaughtCoroutineException(context, handlerException(exception, t))
27+
handleUncaughtCoroutineException(context, handlerException(reportException, t))
2728
return
2829
}
2930
// If a handler is not present in the context or an exception was thrown, fallback to the global handler
30-
handleUncaughtCoroutineException(context, exception)
31+
handleUncaughtCoroutineException(context, reportException)
3132
}
3233

3334
internal fun handlerException(originalException: Throwable, thrownException: Throwable): Throwable {

kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ private fun dispatcherFailure(completion: Continuation<*>, e: Throwable) {
6060
* 2) Rethrow the exception immediately, so it will crash the caller (e.g. when the coroutine had
6161
* no parent or it was async/produce over MainScope).
6262
*/
63-
completion.resumeWith(Result.failure(e))
64-
throw e
63+
val reportException = if (e is DispatchException) e.cause!! else e
64+
completion.resumeWith(Result.failure(reportException))
65+
throw reportException
6566
}

0 commit comments

Comments
 (0)