diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt index 9c66efc2c32..99b5300d44f 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment.kt @@ -509,10 +509,18 @@ class KotlinCoreEnvironment private constructor( // Only valid use-case is when Application should be cached to avoid // initialization costs if (CompilerSystemProperties.KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY.value.toBooleanLenient() != true) { - Disposer.register(parentDisposable, Disposable { - synchronized(APPLICATION_LOCK) { - if (--ourProjectCount <= 0) { - disposeApplicationEnvironment() + // Disposer uses identity of passed object to deduplicate registered disposables + // We should everytime pass new instance to avoid un-registering from previous one + @Suppress("ObjectLiteralToLambda") + Disposer.register(parentDisposable, object : Disposable { + override fun dispose() { + synchronized(APPLICATION_LOCK) { + // Build-systems may run many instances of the compiler in parallel + // All projects share the same ApplicationEnvironment, and when the last project is disposed, + // the ApplicationEnvironment is disposed as well + if (--ourProjectCount <= 0) { + disposeApplicationEnvironment() + } } } })