aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/common/src/intrinsics
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2021-08-23 23:55:41 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2021-08-23 23:55:41 +0000
commitffe1b7dc19f3fbe1bc4a5f4f1e28c64cd68c5371 (patch)
tree747e5be83c6e6a2e2d9a164ebd03c5e45e6ec952 /kotlinx-coroutines-core/common/src/intrinsics
parent2bbecd60d31b5d0b48847f67e741018f5ae7aa07 (diff)
parent531d55eaeac09ad6cde55687ef802d4235040985 (diff)
downloadplatform_external_kotlinx.coroutines-simpleperf-release.tar.gz
platform_external_kotlinx.coroutines-simpleperf-release.tar.bz2
platform_external_kotlinx.coroutines-simpleperf-release.zip
Snap for 7668063 from 531d55eaeac09ad6cde55687ef802d4235040985 to simpleperf-releasesimpleperf-release
Change-Id: I844c9c61f2e31a265c36a483e1509e0e08c508d6
Diffstat (limited to 'kotlinx-coroutines-core/common/src/intrinsics')
-rw-r--r--kotlinx-coroutines-core/common/src/intrinsics/Cancellable.kt15
-rw-r--r--kotlinx-coroutines-core/common/src/intrinsics/Undispatched.kt8
2 files changed, 17 insertions, 6 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
+}
diff --git a/kotlinx-coroutines-core/common/src/intrinsics/Undispatched.kt b/kotlinx-coroutines-core/common/src/intrinsics/Undispatched.kt
index 1273634e..38e870ef 100644
--- a/kotlinx-coroutines-core/common/src/intrinsics/Undispatched.kt
+++ b/kotlinx-coroutines-core/common/src/intrinsics/Undispatched.kt
@@ -82,11 +82,9 @@ private inline fun <T> startDirect(completion: Continuation<T>, block: (Continua
* This function shall be invoked at most once on this coroutine.
* This function checks cancellation of the outer [Job] on fast-path.
*
- * First, this function initializes the parent job from the `parentContext` of this coroutine that was passed to it
- * during construction. Second, it starts the coroutine using [startCoroutineUninterceptedOrReturn].
+ * It starts the coroutine using [startCoroutineUninterceptedOrReturn].
*/
internal fun <T, R> ScopeCoroutine<T>.startUndispatchedOrReturn(receiver: R, block: suspend R.() -> T): Any? {
- initParentJob()
return undispatchedResult({ true }) {
block.startCoroutineUninterceptedOrReturn(receiver, this)
}
@@ -96,8 +94,8 @@ internal fun <T, R> ScopeCoroutine<T>.startUndispatchedOrReturn(receiver: R, blo
* Same as [startUndispatchedOrReturn], but ignores [TimeoutCancellationException] on fast-path.
*/
internal fun <T, R> ScopeCoroutine<T>.startUndispatchedOrReturnIgnoreTimeout(
- receiver: R, block: suspend R.() -> T): Any? {
- initParentJob()
+ receiver: R, block: suspend R.() -> T
+): Any? {
return undispatchedResult({ e -> !(e is TimeoutCancellationException && e.coroutine === this) }) {
block.startCoroutineUninterceptedOrReturn(receiver, this)
}