Skip to content

Commit

Permalink
Fix duplicating class loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Apr 28, 2022
1 parent 931afa8 commit f5df30d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
import kotlin.script.experimental.jvm.impl.KJvmCompiledScript
import kotlin.script.experimental.jvm.impl.getOrCreateActualClassloader
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvm.lastSnippetClassLoader
import kotlin.script.experimental.jvm.util.toSourceCodePosition
import kotlin.script.experimental.util.LinkedSnippet

Expand Down Expand Up @@ -122,9 +123,11 @@ open class JupyterCompilerImpl<CompilerT : ReplCompiler<KJvmCompiledScript>>(
override val lastKClass: KClass<*>
get() = classes.last()

private val _baseClassLoader: ClassLoader
get() = basicEvaluationConfiguration[ScriptEvaluationConfiguration.jvm.baseClassLoader]!!

override val lastClassLoader: ClassLoader
get() = classes.lastOrNull()?.java?.classLoader
?: basicEvaluationConfiguration[ScriptEvaluationConfiguration.jvm.baseClassLoader]!!
get() = classes.lastOrNull()?.java?.classLoader ?: _baseClassLoader

override fun nextCounter() = executionCounter.getAndIncrement()

Expand Down Expand Up @@ -174,11 +177,9 @@ open class JupyterCompilerImpl<CompilerT : ReplCompiler<KJvmCompiledScript>>(
val compiledScript = result.get()

val configWithClassloader = basicEvaluationConfiguration.with {
val lastClassOrNull = classes.lastOrNull()
if (lastClassOrNull != null) {
jvm {
baseClassLoader(lastClassOrNull.java.classLoader)
}
jvm {
lastSnippetClassLoader(lastClassLoader)
baseClassLoader(_baseClassLoader.parent)
}
}
val classLoader = compiledScript.getOrCreateActualClassloader(configWithClassloader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
import java.net.URLClassLoader
import kotlin.concurrent.thread
import kotlin.test.assertEquals

Expand Down Expand Up @@ -46,6 +47,12 @@ class ReplWithStandardResolverTests : AbstractSingleReplTest() {

@Test
fun testStandardLibraryResolver() {
val baseClassLoader = repl.currentClassLoader.parent
fun urlClassLoadersCount() = generateSequence(repl.currentClassLoader) { classLoader ->
classLoader.parent?.takeIf { it != baseClassLoader }
}.filter { it is URLClassLoader }.count()
urlClassLoadersCount() shouldBe 1

val res = eval(
"""
%use krangl@d91d045946f59(0.16.2)
Expand All @@ -54,6 +61,10 @@ class ReplWithStandardResolverTests : AbstractSingleReplTest() {
""".trimIndent()
)
assertEquals("John Smith", res.resultValue)
urlClassLoadersCount() shouldBe 2

eval("val x = 2 + 2")
urlClassLoadersCount() shouldBe 2
}

@Test
Expand Down

0 comments on commit f5df30d

Please sign in to comment.