Skip to content

Commit

Permalink
Refactoring: Improve DispatchedTask.run a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov committed Sep 16, 2020
1 parent 85b1a2b commit 074ea3d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt
Expand Up @@ -41,19 +41,22 @@ internal abstract class DispatchedTask<in T>(
val state = takeState() // NOTE: Must take state in any case, even if cancelled
withCoroutineContext(context, delegate.countOrElement) {
val exception = getExceptionalResult(state)
val job = if (resumeMode.isCancellableMode) context[Job] else null
/*
* Check whether continuation was originally resumed with an exception.
* If so, it dominates cancellation, otherwise the original exception
* will be silently lost.
*/
if (exception == null && job != null && !job.isActive) {
val job = if (exception == null && resumeMode.isCancellableMode) context[Job] else null
if (job != null && !job.isActive) {
val cause = job.getCancellationException()
cancelResult(state, cause)
continuation.resumeWithStackTrace(cause)
} else {
if (exception != null) continuation.resumeWithException(exception)
else continuation.resume(getSuccessfulResult(state))
if (exception != null) {
continuation.resumeWithException(exception)
} else {
continuation.resume(getSuccessfulResult(state))
}
}
}
} catch (e: Throwable) {
Expand Down

0 comments on commit 074ea3d

Please sign in to comment.