diff --git a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java index 14aad06748c..cf4348b2846 100644 --- a/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java +++ b/core/util.runtime/src/org/jetbrains/kotlin/storage/LockBasedStorageManager.java @@ -591,11 +591,24 @@ public class LockBasedStorageManager implements StorageManager { } catch (Throwable throwable) { if (ExceptionUtilsKt.isProcessCanceledException(throwable)) { - cache.remove(input); + Object remove; + try { + remove = cache.remove(input); + } catch (Throwable e) { + throw unableToRemoveKey(input, e); + } + if (remove != NotValue.COMPUTING) { + throw inconsistentComputingKey(input, remove); + } //noinspection ConstantConditions throw (RuntimeException)throwable; } if (throwable == error) { + try { + cache.remove(input); + } catch (Throwable e) { + throw unableToRemoveKey(input, e); + } throw storageManager.exceptionHandlingStrategy.handleException(throwable); } @@ -625,6 +638,22 @@ public class LockBasedStorageManager implements StorageManager { ); } + private AssertionError inconsistentComputingKey(K input, Object oldValue) { + return sanitizeStackTrace( + new AssertionError("Inconsistent key detected. " + + NotValue.COMPUTING + " is expected, was: " + oldValue + + ", most probably race condition detected on input " + input + + " under " + storageManager) + ); + } + + private AssertionError unableToRemoveKey(K input, Throwable throwable) { + return sanitizeStackTrace( + new AssertionError("Unable to remove " + + input + " under " + storageManager, throwable) + ); + } + @Override public boolean isComputed(K key) { Object value = cache.get(key);