From 53069ee03f4f613c0ef46c35728ac402b50e3ac9 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Tue, 30 Aug 2022 18:28:41 +0200 Subject: [PATCH] Fix application disposer registered only once due to deduplication Disposer uses passed object identity to decide should it be registered Since lambda, that passed into disposer doesn't capture anything, it has only one instance, so it ends up being registered only once While it should be registered for every parentDisposable --- .../cli/jvm/compiler/KotlinCoreEnvironment.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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() + } } } })