From dbc54bc5a8f10b4aef616ac69a8f54ee2d935323 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 4 Jun 2018 18:03:14 +0300 Subject: [PATCH] restore order: first validate nested index, then disable uptodate check, then take the lock to avoid deadlock (for IDEA-CR-33212) --- .../com/intellij/psi/stubs/StubIndexImpl.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java index 24f69d1ff5b7..549c94693df0 100644 --- a/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/stubs/StubIndexImpl.java @@ -20,6 +20,7 @@ import com.intellij.openapi.vfs.newvfs.ManagingFS; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFS; import com.intellij.psi.PsiElement; import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.util.ConcurrencyUtil; import com.intellij.util.Processor; import com.intellij.util.Processors; import com.intellij.util.SmartList; @@ -334,16 +335,18 @@ public class StubIndexImpl extends StubIndex implements PersistentStateComponent UpdatableIndex stubUpdatingIndex = fileBasedIndex.getIndex(stubUpdatingIndexId); try { - stubUpdatingIndex.getReadLock().lock(); - try { - // disable up-to-date check to avoid locks on attempt to acquire index write lock while holding at the same time the readLock for this index - return FileBasedIndexImpl.disableUpToDateCheckIn(()-> - myAccessValidator.validate(stubUpdatingIndexId, ()->index.getData(key).forEach(action))); - } - finally { - stubUpdatingIndex.getReadLock().unlock(); - wipeProblematicFileIdsForParticularKeyAndStubIndex(indexKey, key, stubUpdatingIndex); - } + return myAccessValidator.validate(stubUpdatingIndexId, ()-> { + try { + return FileBasedIndexImpl.disableUpToDateCheckIn(() -> + ConcurrencyUtil.withLock(stubUpdatingIndex.getReadLock(), () -> + // disable up-to-date check to avoid locks on attempt to acquire index write lock while holding at the same time the readLock for this index + index.getData(key).forEach(action) + )); + } + finally { + wipeProblematicFileIdsForParticularKeyAndStubIndex(indexKey, key, stubUpdatingIndex); + } + }); } catch (StorageException e) { forceRebuild(e);