Skip to content

Eagerly load CoroutineExceptionHandler and load corresponding service #2957

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

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines

import kotlin.coroutines.*
import kotlin.jvm.*

internal expect fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable)

/**
* JVM kludge: trigger loading of all the classes and service loading
* **before** any exception occur because it may be OOM, SOE or VerifyError
*/
internal expect fun initializeDefaultExceptionHandlers()

/**
* Helper function for coroutine builder implementations to handle uncaught and unexpected exceptions in coroutines,
* that could not be otherwise handled in a normal way through structured concurrency, saving them to a future, and
Expand Down
6 changes: 5 additions & 1 deletion kotlinx-coroutines-core/common/src/Job.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ public interface Job : CoroutineContext.Element {
/**
* Key for [Job] instance in the coroutine context.
*/
public companion object Key : CoroutineContext.Key<Job>
public companion object Key : CoroutineContext.Key<Job> {
init {
initializeDefaultExceptionHandlers()
}
}

// ------------ state query ------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package kotlinx.coroutines

import kotlin.coroutines.*

internal actual fun initializeDefaultExceptionHandlers() {
// Do nothing
}

internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
// log exception
console.error(exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ private val handlers: List<CoroutineExceptionHandler> = ServiceLoader.load(
CoroutineExceptionHandler::class.java.classLoader
).iterator().asSequence().toList()

internal actual fun initializeDefaultExceptionHandlers() {
// Load CEH and handlers
CoroutineExceptionHandler
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect Class.forName or something here instead. As is, could you please explain due to what guarantees does this not get thrown away as dead code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JVM has no right to throw away any code with side effects. Classloading is not pure, GETSTATIC CEH.Key will trigger classloading:

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.4.3

Also, the key here is to initialize handlers which are initialized when initializeDefaultExceptionHandlers first accessed (because these symbols belong to the same class)

}


internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
// use additional extension handlers
for (handler in handlers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package kotlinx.coroutines

import kotlin.coroutines.*

internal actual fun initializeDefaultExceptionHandlers() {
// Do nothing
}

internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
// log exception
exception.printStackTrace()
Expand Down