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
This commit is contained in:
Simon Ogorodnik
2022-08-30 18:28:41 +02:00
committed by teamcity
parent f2dee2bf85
commit 53069ee03f
@@ -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()
}
}
}
})