diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/CleanableSoftValueCache.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/CleanableSoftValueCache.kt index d86bbe8a138..6b875cdccf0 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/CleanableSoftValueCache.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/caches/CleanableSoftValueCache.kt @@ -61,8 +61,12 @@ class CleanableSoftValueCache( @Suppress("UNCHECKED_CAST") ref as SoftReferenceWithCleanup - backingMap.remove(ref.key, ref) - ref.performCleanup() + val wasRemoved = backingMap.remove(ref.key, ref) + + // If `ref` already wasn't part of the map, it will have been cleaned up by a deterministic removal operation. + if (wasRemoved) { + ref.performCleanup() + } } }