Skip to content

Commit

Permalink
Create Dispatcher.Default threads with the same context classloader a… (
Browse files Browse the repository at this point in the history
#3877)

* Create Dispatcher.Default threads with the same context classloader as the dispatcher itself

In order to properly operate in modularized on a classloader level environments with the absence of other workarounds (i.e. supplying application-specific thread factory)

* Do the same trick for DefaultExecutor

Fixes #3832
  • Loading branch information
qwwdfsad committed Nov 30, 2023
1 parent 00a0767 commit a79db37
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions kotlinx-coroutines-core/jvm/src/DefaultExecutor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ internal actual object DefaultExecutor : EventLoopImplBase(), Runnable {
private fun createThreadSync(): Thread {
return _thread ?: Thread(this, THREAD_NAME).apply {
_thread = this
/*
* `DefaultExecutor` is a global singleton that creates its thread lazily.
* To isolate the classloaders properly, we are inherting the context classloader from
* the singleton itself instead of using parent' thread one
* in order not to accidentally capture temporary application classloader.
*/
contextClassLoader = this@DefaultExecutor.javaClass.classLoader
isDaemon = true
start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,13 @@ internal class CoroutineScheduler(
internal inner class Worker private constructor() : Thread() {
init {
isDaemon = true
/*
* `Dispatchers.Default` is used as *the* dispatcher in the containerized environments,
* isolated by their own classloaders. Workers are populated lazily, thus we are inheriting
* `Dispatchers.Default` context class loader here instead of using parent' thread one
* in order not to accidentally capture temporary application classloader.
*/
contextClassLoader = this@CoroutineScheduler.javaClass.classLoader
}

// guarded by scheduler lock, index in workers array, 0 when not in array (terminated)
Expand Down

0 comments on commit a79db37

Please sign in to comment.