From cfb3ff92f175c6ef66e6ed3257cdd52262031966 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Mon, 16 Mar 2020 12:36:08 +0300 Subject: [PATCH] Fill in stacktrace of cancellation exception with an empty array to properly work on Android <= 6.0 that had a bug on a code-path with an empty stacktrace Fixes #1866 --- kotlinx-coroutines-core/jvm/src/Exceptions.kt | 3 ++- .../jvm/src/flow/internal/FlowExceptions.kt | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/kotlinx-coroutines-core/jvm/src/Exceptions.kt b/kotlinx-coroutines-core/jvm/src/Exceptions.kt index 22a02a30bb..0684ce2397 100644 --- a/kotlinx-coroutines-core/jvm/src/Exceptions.kt +++ b/kotlinx-coroutines-core/jvm/src/Exceptions.kt @@ -40,7 +40,8 @@ internal actual class JobCancellationException public actual constructor( if (DEBUG) { return super.fillInStackTrace() } - + // Prevent Android <= 6.0 bug, #1866 + stackTrace = emptyArray() /* * In non-debug mode we don't want to have a stacktrace on every cancellation/close, * parent job reference is enough. Stacktrace of JCE is not needed most of the time (e.g., it is not logged) diff --git a/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt b/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt index 863c2cc4ab..4fb958ac7d 100644 --- a/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt +++ b/kotlinx-coroutines-core/jvm/src/flow/internal/FlowExceptions.kt @@ -12,14 +12,18 @@ internal actual class AbortFlowException actual constructor( ) : CancellationException("Flow was aborted, no more elements needed") { override fun fillInStackTrace(): Throwable { - if (DEBUG) super.fillInStackTrace() + if (DEBUG) return super.fillInStackTrace() + // Prevent Android <= 6.0 bug, #1866 + stackTrace = emptyArray() return this } } internal actual class ChildCancelledException : CancellationException("Child of the scoped flow was cancelled") { override fun fillInStackTrace(): Throwable { - if (DEBUG) super.fillInStackTrace() + if (DEBUG) return super.fillInStackTrace() + // Prevent Android <= 6.0 bug, #1866 + stackTrace = emptyArray() return this } }