Scripting: implement correct disposal of REPL state on finalization
#KT-47927 fixed
This commit is contained in:
committed by
TeamCityServer
parent
a45e31720c
commit
4243bafd1d
+25
@@ -8,7 +8,9 @@ package kotlin.script.experimental.jsr223.test
|
|||||||
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
|
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
|
||||||
import org.jetbrains.kotlin.config.KotlinCompilerVersion
|
import org.jetbrains.kotlin.config.KotlinCompilerVersion
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
|
import org.junit.Ignore
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
import java.lang.management.ManagementFactory
|
||||||
import javax.script.*
|
import javax.script.*
|
||||||
import kotlin.script.experimental.jvmhost.jsr223.KotlinJsr223ScriptEngineImpl
|
import kotlin.script.experimental.jvmhost.jsr223.KotlinJsr223ScriptEngineImpl
|
||||||
|
|
||||||
@@ -71,6 +73,29 @@ class KotlinJsr223ScriptEngineIT {
|
|||||||
Assert.assertEquals(5, res2)
|
Assert.assertEquals(5, res2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore // Probably not possible to make it sensible on CI and with parallel run, so leaving it here for manual testing only
|
||||||
|
fun testMemory() {
|
||||||
|
val memoryMXBean = ManagementFactory.getMemoryMXBean()!!
|
||||||
|
var prevMem = memoryMXBean.getHeapMemoryUsage().getUsed()
|
||||||
|
for (i in 1..10) {
|
||||||
|
with(ScriptEngineManager().getEngineByExtension("kts")) {
|
||||||
|
val res1 = eval("val x = 3")
|
||||||
|
Assert.assertNull(res1)
|
||||||
|
val res2 = eval("x + 2")
|
||||||
|
Assert.assertEquals(5, res2)
|
||||||
|
}
|
||||||
|
System.gc()
|
||||||
|
val curMem = memoryMXBean.getHeapMemoryUsage().getUsed()
|
||||||
|
if (i > 3 && curMem > prevMem) {
|
||||||
|
Assert.assertTrue("Memory leak: iter: $i prev: $prevMem, cur: $curMem", (curMem - prevMem) < 1024*1024 )
|
||||||
|
}
|
||||||
|
println("${curMem/1024/1024}Mb")
|
||||||
|
prevMem = curMem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testIncomplete() {
|
fun testIncomplete() {
|
||||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||||
|
|||||||
+3
-1
@@ -6,6 +6,7 @@
|
|||||||
package kotlin.script.experimental.jvmhost.jsr223
|
package kotlin.script.experimental.jvmhost.jsr223
|
||||||
|
|
||||||
import org.jetbrains.kotlin.cli.common.repl.*
|
import org.jetbrains.kotlin.cli.common.repl.*
|
||||||
|
import java.lang.ref.WeakReference
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||||
import javax.script.ScriptContext
|
import javax.script.ScriptContext
|
||||||
import javax.script.ScriptEngineFactory
|
import javax.script.ScriptEngineFactory
|
||||||
@@ -32,8 +33,9 @@ class KotlinJsr223ScriptEngineImpl(
|
|||||||
private var lastScriptContext: ScriptContext? = null
|
private var lastScriptContext: ScriptContext? = null
|
||||||
|
|
||||||
val jsr223HostConfiguration = ScriptingHostConfiguration(defaultJvmScriptingHostConfiguration) {
|
val jsr223HostConfiguration = ScriptingHostConfiguration(defaultJvmScriptingHostConfiguration) {
|
||||||
|
val weakThis = WeakReference(this@KotlinJsr223ScriptEngineImpl)
|
||||||
jsr223 {
|
jsr223 {
|
||||||
getScriptContext { lastScriptContext ?: getContext() }
|
getScriptContext { weakThis.get()?.let { it.lastScriptContext ?: it.getContext() } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
@@ -37,6 +37,10 @@ class JvmReplCompilerState<CompilationT : JvmReplCompilerState.Compilation>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected fun finalize() {
|
||||||
|
dispose()
|
||||||
|
}
|
||||||
|
|
||||||
fun getCompilationState(scriptCompilationConfiguration: ScriptCompilationConfiguration): CompilationT = lock.write {
|
fun getCompilationState(scriptCompilationConfiguration: ScriptCompilationConfiguration): CompilationT = lock.write {
|
||||||
if (_compilation == null) {
|
if (_compilation == null) {
|
||||||
initializeCompilation(scriptCompilationConfiguration)
|
initializeCompilation(scriptCompilationConfiguration)
|
||||||
|
|||||||
Reference in New Issue
Block a user