diff options
Diffstat (limited to 'kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt')
-rw-r--r-- | kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt b/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt index 173f0afb..f5b96a8d 100644 --- a/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt +++ b/kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt @@ -49,6 +49,19 @@ private inline fun runSafely(completion: Continuation<*>, block: () -> Unit) { try { block() } catch (e: Throwable) { - completion.resumeWith(Result.failure(e)) + dispatcherFailure(completion, e) } } + +private fun dispatcherFailure(completion: Continuation<*>, e: Throwable) { + /* + * This method is invoked when we failed to start a coroutine due to the throwing + * dispatcher implementation or missing Dispatchers.Main. + * This situation is not recoverable, so we are trying to deliver the exception by all means: + * 1) Resume the coroutine with an exception, so it won't prevent its parent from completion + * 2) Rethrow the exception immediately, so it will crash the caller (e.g. when the coroutine had + * no parent or it was async/produce over MainScope). + */ + completion.resumeWith(Result.failure(e)) + throw e +} |