Skip to content

Commit bd44a94

Browse files
qwwdfsadyorickhenning
authored andcommitted
Eagerly load CoroutineExceptionHandler and load corresponding service (Kotlin#2957)
* Eagerly load CoroutineExceptionHandler and load the corresponding service Partially addresses Kotlin#2552
1 parent 5b4b612 commit bd44a94

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
/*
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
4-
54
package kotlinx.coroutines
65

76
import kotlin.coroutines.*
7+
import kotlin.jvm.*
88

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

11+
/**
12+
* JVM kludge: trigger loading of all the classes and service loading
13+
* **before** any exception occur because it may be OOM, SOE or VerifyError
14+
*/
15+
internal expect fun initializeDefaultExceptionHandlers()
16+
1117
/**
1218
* Helper function for coroutine builder implementations to handle uncaught and unexpected exceptions in coroutines,
1319
* that could not be otherwise handled in a normal way through structured concurrency, saving them to a future, and

kotlinx-coroutines-core/common/src/Job.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ public interface Job : CoroutineContext.Element {
113113
/**
114114
* Key for [Job] instance in the coroutine context.
115115
*/
116-
public companion object Key : CoroutineContext.Key<Job>
116+
public companion object Key : CoroutineContext.Key<Job> {
117+
init {
118+
// `Job` will necessarily be accessed early, so this is as good a place as any for the
119+
// initialization logic that we want to happen as soon as possible
120+
initializeDefaultExceptionHandlers()
121+
}
122+
}
117123

118124
// ------------ state query ------------
119125

kotlinx-coroutines-core/js/src/CoroutineExceptionHandlerImpl.kt

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ package kotlinx.coroutines
66

77
import kotlin.coroutines.*
88

9+
internal actual fun initializeDefaultExceptionHandlers() {
10+
// Do nothing
11+
}
12+
913
internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
1014
// log exception
1115
console.error(exception)

kotlinx-coroutines-core/jvm/src/CoroutineExceptionHandlerImpl.kt

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ private val handlers: List<CoroutineExceptionHandler> = ServiceLoader.load(
2222
CoroutineExceptionHandler::class.java.classLoader
2323
).iterator().asSequence().toList()
2424

25+
internal actual fun initializeDefaultExceptionHandlers() {
26+
// Load CEH and handlers
27+
CoroutineExceptionHandler
28+
}
29+
30+
2531
internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
2632
// use additional extension handlers
2733
for (handler in handlers) {

kotlinx-coroutines-core/native/src/CoroutineExceptionHandlerImpl.kt

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ package kotlinx.coroutines
66

77
import kotlin.coroutines.*
88

9+
internal actual fun initializeDefaultExceptionHandlers() {
10+
// Do nothing
11+
}
12+
913
internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
1014
// log exception
1115
exception.printStackTrace()

0 commit comments

Comments
 (0)