-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In the test module, collect and report the actual uncaught exceptions #3449
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
kotlinx-coroutines-core/common/src/internal/CoroutineExceptionHandlerImpl.common.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* The list of globally installed [CoroutineExceptionHandler] instances that will be notified of any exceptions that | ||
* were not processed in any other manner. | ||
*/ | ||
internal expect val platformExceptionHandlers: Collection<CoroutineExceptionHandler> | ||
|
||
/** | ||
* Ensures that the given [callback] is present in the [platformExceptionHandlers] list. | ||
*/ | ||
internal expect fun ensurePlatformExceptionHandlerLoaded(callback: CoroutineExceptionHandler) | ||
|
||
/** | ||
* The platform-dependent global exception handler, used so that the exception is logged at least *somewhere*. | ||
*/ | ||
internal expect fun propagateExceptionFinalResort(exception: Throwable) | ||
|
||
/** | ||
* Deal with exceptions that happened in coroutines and weren't programmatically dealt with. | ||
* | ||
* First, it notifies every [CoroutineExceptionHandler] in the [platformExceptionHandlers] list. | ||
* If one of them throws [ExceptionSuccessfullyProcessed], it means that that handler believes that the exception was | ||
* dealt with sufficiently well and doesn't need any further processing. | ||
* Otherwise, the platform-dependent global exception handler is also invoked. | ||
*/ | ||
internal fun handleUncaughtCoroutineException(context: CoroutineContext, exception: Throwable) { | ||
// use additional extension handlers | ||
for (handler in platformExceptionHandlers) { | ||
try { | ||
handler.handleException(context, exception) | ||
} catch (_: ExceptionSuccessfullyProcessed) { | ||
return | ||
} catch (t: Throwable) { | ||
propagateExceptionFinalResort(handlerException(exception, t)) | ||
} | ||
} | ||
|
||
try { | ||
exception.addSuppressed(DiagnosticCoroutineContextException(context)) | ||
} catch (e: Throwable) { | ||
// addSuppressed is never user-defined and cannot normally throw with the only exception being OOM | ||
// we do ignore that just in case to definitely deliver the exception | ||
} | ||
propagateExceptionFinalResort(exception) | ||
} | ||
|
||
/** | ||
* Private exception that is added to suppressed exceptions of the original exception | ||
* when it is reported to the last-ditch current thread 'uncaughtExceptionHandler'. | ||
* | ||
* The purpose of this exception is to add an otherwise inaccessible diagnostic information and to | ||
* be able to poke the context of the failing coroutine in the debugger. | ||
*/ | ||
internal expect class DiagnosticCoroutineContextException(context: CoroutineContext) : RuntimeException | ||
|
||
/** | ||
* A dummy exception that signifies that the exception was successfully processed by the handler and no further | ||
* action is required. | ||
* | ||
* Would be nicer if [CoroutineExceptionHandler] could return a boolean, but that would be a breaking change. | ||
* For now, we will take solace in knowledge that such exceptions are exceedingly rare, even rarer than globally | ||
* uncaught exceptions in general. | ||
*/ | ||
internal object ExceptionSuccessfullyProcessed : Exception() |
12 changes: 0 additions & 12 deletions
12
kotlinx-coroutines-core/js/src/CoroutineExceptionHandlerImpl.kt
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
kotlinx-coroutines-core/js/src/internal/CoroutineExceptionHandlerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
private val platformExceptionHandlers_ = mutableSetOf<CoroutineExceptionHandler>() | ||
|
||
internal actual val platformExceptionHandlers: Collection<CoroutineExceptionHandler> | ||
get() = platformExceptionHandlers_ | ||
|
||
internal actual fun ensurePlatformExceptionHandlerLoaded(callback: CoroutineExceptionHandler) { | ||
platformExceptionHandlers_ += callback | ||
} | ||
|
||
internal actual fun propagateExceptionFinalResort(exception: Throwable) { | ||
// log exception | ||
console.error(exception) | ||
} | ||
|
||
internal actual class DiagnosticCoroutineContextException actual constructor(context: CoroutineContext) : | ||
RuntimeException(context.toString()) | ||
|
62 changes: 0 additions & 62 deletions
62
kotlinx-coroutines-core/jvm/src/CoroutineExceptionHandlerImpl.kt
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
kotlinx-coroutines-core/jvm/src/internal/CoroutineExceptionHandlerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import java.util.* | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
/** | ||
* A list of globally installed [CoroutineExceptionHandler] instances. | ||
* | ||
* Note that Android may have dummy [Thread.contextClassLoader] which is used by one-argument [ServiceLoader.load] function, | ||
* see (https://stackoverflow.com/questions/13407006/android-class-loader-may-fail-for-processes-that-host-multiple-applications). | ||
* So here we explicitly use two-argument `load` with a class-loader of [CoroutineExceptionHandler] class. | ||
* | ||
* We are explicitly using the `ServiceLoader.load(MyClass::class.java, MyClass::class.java.classLoader).iterator()` | ||
* form of the ServiceLoader call to enable R8 optimization when compiled on Android. | ||
*/ | ||
internal actual val platformExceptionHandlers: Collection<CoroutineExceptionHandler> = ServiceLoader.load( | ||
CoroutineExceptionHandler::class.java, | ||
CoroutineExceptionHandler::class.java.classLoader | ||
).iterator().asSequence().toList() | ||
|
||
internal actual fun ensurePlatformExceptionHandlerLoaded(callback: CoroutineExceptionHandler) { | ||
// we use JVM's mechanism of ServiceLoader, so this should be a no-op on JVM. | ||
// The only thing we do is make sure that the ServiceLoader did work correctly. | ||
check(callback in platformExceptionHandlers) { "Exception handler was not found via a ServiceLoader" } | ||
} | ||
|
||
internal actual fun propagateExceptionFinalResort(exception: Throwable) { | ||
// use the thread's handler | ||
val currentThread = Thread.currentThread() | ||
currentThread.uncaughtExceptionHandler.uncaughtException(currentThread, exception) | ||
} | ||
|
||
// This implementation doesn't store a stacktrace, which is good because a stacktrace doesn't make sense for this. | ||
internal actual class DiagnosticCoroutineContextException actual constructor(@Transient private val context: CoroutineContext) : RuntimeException() { | ||
override fun getLocalizedMessage(): String { | ||
return context.toString() | ||
} | ||
|
||
override fun fillInStackTrace(): Throwable { | ||
// Prevent Android <= 6.0 bug, #1866 | ||
stackTrace = emptyArray() | ||
return this | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
kotlinx-coroutines-core/native/src/CoroutineExceptionHandlerImpl.kt
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
kotlinx-coroutines-core/native/src/internal/CoroutineExceptionHandlerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.internal | ||
|
||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
import kotlin.native.* | ||
|
||
private val lock = SynchronizedObject() | ||
|
||
internal actual val platformExceptionHandlers: Collection<CoroutineExceptionHandler> | ||
get() = synchronized(lock) { platformExceptionHandlers_ } | ||
|
||
private val platformExceptionHandlers_ = mutableSetOf<CoroutineExceptionHandler>() | ||
|
||
internal actual fun ensurePlatformExceptionHandlerLoaded(callback: CoroutineExceptionHandler) { | ||
synchronized(lock) { | ||
platformExceptionHandlers_ += callback | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
internal actual fun propagateExceptionFinalResort(exception: Throwable) { | ||
// log exception | ||
processUnhandledException(exception) | ||
} | ||
|
||
internal actual class DiagnosticCoroutineContextException actual constructor(context: CoroutineContext) : | ||
RuntimeException(context.toString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like callbacks are never removed for non-legacy version, consuming the memory lineary to number of tests run, could you please address that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. You're right, of course. I simply rebased the code on
develop
but totally forgot that theleave
function has split in two and needs changes in both places.