From 02ab5e63451578afd60a22d85e16f73fc051ffe7 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Wed, 2 Oct 2019 12:06:35 +0200 Subject: [PATCH] keep track of file being requested to be indexed this change allows code to avoid deadlock when FileTypeSubstitutor depends on index value. File being requested to be indexed is returned from FileBasedIndex.getFileBeingCurrentlyIndexed() GitOrigin-RevId: 8c0170e4076cc64a51dc1196d6924e523eb0ca78 --- .../util/indexing/FileBasedIndexImpl.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index 94d886421c7f..9efd35300c10 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -106,6 +106,7 @@ import java.util.stream.Stream; */ public final class FileBasedIndexImpl extends FileBasedIndex { private static final ThreadLocal ourIndexedFile = new ThreadLocal<>(); + private static final ThreadLocal ourFileToBeIndexed = new ThreadLocal<>(); static final Logger LOG = Logger.getInstance("#com.intellij.util.indexing.FileBasedIndexImpl"); private static final String CORRUPTION_MARKER_NAME = "corruption.marker"; private static final NotificationGroup NOTIFICATIONS = new NotificationGroup("Indexing", NotificationDisplayType.BALLOON, false); @@ -1725,7 +1726,8 @@ public final class FileBasedIndexImpl extends FileBasedIndex { @Override public VirtualFile getFileBeingCurrentlyIndexed() { - return ourIndexedFile.get(); + VirtualFile file = ourIndexedFile.get(); + return file != null ? file : ourFileToBeIndexed.get(); } private class VirtualFileUpdateTask extends UpdateTask { @@ -1878,28 +1880,33 @@ public final class FileBasedIndexImpl extends FileBasedIndex { getChangedFilesCollector().removeScheduledFileFromUpdate(file); } else { - getFileTypeManager().freezeFileTypeTemporarilyIn(file, () -> { - final List> candidates = getAffectedIndexCandidates(file); + ourFileToBeIndexed.set(file); + try { + getFileTypeManager().freezeFileTypeTemporarilyIn(file, () -> { + final List> candidates = getAffectedIndexCandidates(file); - boolean scheduleForUpdate = false; + boolean scheduleForUpdate = false; - //noinspection ForLoopReplaceableByForEach - for (int i = 0, size = candidates.size(); i < size; ++i) { - final ID indexId = candidates.get(i); - if (needsFileContentLoading(indexId) && getInputFilter(indexId).acceptInput(file)) { - getIndex(indexId).resetIndexedStateForFile(fileId); - scheduleForUpdate = true; + //noinspection ForLoopReplaceableByForEach + for (int i = 0, size = candidates.size(); i < size; ++i) { + final ID indexId = candidates.get(i); + if (needsFileContentLoading(indexId) && getInputFilter(indexId).acceptInput(file)) { + getIndex(indexId).resetIndexedStateForFile(fileId); + scheduleForUpdate = true; + } } - } - if (scheduleForUpdate) { - IndexingStamp.flushCache(fileId); - getChangedFilesCollector().scheduleForUpdate(file); - } - else if (file instanceof VirtualFileSystemEntry) { - ((VirtualFileSystemEntry)file).setFileIndexed(true); - } - }); + if (scheduleForUpdate) { + IndexingStamp.flushCache(fileId); + getChangedFilesCollector().scheduleForUpdate(file); + } + else if (file instanceof VirtualFileSystemEntry) { + ((VirtualFileSystemEntry)file).setFileIndexed(true); + } + }); + } finally { + ourFileToBeIndexed.remove(); + } } } }