Skip to content

Commit 990feca

Browse files
elizarovqwwdfsad
authored andcommitted
Use explicit classloader when loading installed CoroutineExceptionHandler impls
Fixes #530
1 parent c430e07 commit 990feca

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@
55
package kotlinx.coroutines.experimental
66

77
import java.util.*
8-
import kotlin.coroutines.experimental.AbstractCoroutineContextElement
9-
import kotlin.coroutines.experimental.CoroutineContext
8+
import kotlin.coroutines.experimental.*
9+
10+
/**
11+
* A list of globally installed [CoroutineExceptionHandler] instances.
12+
*
13+
* Note, that Android may have dummy [Thread.contextClassLoader] which is used by one-argument [ServiceLoader.load] function,
14+
* see (https://stackoverflow.com/questions/13407006/android-class-loader-may-fail-for-processes-that-host-multiple-applications).
15+
* So here we explicitly use two-argument `load` with a class-loader of [CoroutineExceptionHandler] class.
16+
*/
17+
private val handlers: List<CoroutineExceptionHandler> = CoroutineExceptionHandler::class.java.let { serviceClass ->
18+
ServiceLoader.load(serviceClass, serviceClass.classLoader).toList()
19+
}
1020

1121
internal actual fun handleCoroutineExceptionImpl(context: CoroutineContext, exception: Throwable) {
1222
// use additional extension handlers
13-
ServiceLoader.load(CoroutineExceptionHandler::class.java).forEach { handler ->
23+
for (handler in handlers) {
1424
handler.handleException(context, exception)
1525
}
1626
// use thread's handler

0 commit comments

Comments
 (0)