Skip to content

On JVM worker threads retain context class loader #3832

Closed
@dovchinnikov

Description

@dovchinnikov

Describe the bug

If a thread happens to start from a thread with different context class loader, then the class loader is retained for an unforeseeable time (at least for IDLE_WORKER_KEEP_ALIVE_NS, may be longer under load). In multi-loader environment this causes a leak.

Provide a Reproducer

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class MyClassLoader : ClassLoader()

fun main() {
  val custom = MyClassLoader()
  runBlocking {
    val current = Thread.currentThread().contextClassLoader
    try {
      Thread.currentThread().contextClassLoader = custom
      launch(Dispatchers.Default) {} // spawn new thread
    }
    finally {
      Thread.currentThread().contextClassLoader = current
    }
  }
  println("A place for breakpoint")
}
image

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions