IndexNotReadyException inside runReadActionInSmartMode

Answered

private @NotNull CancellablePromise<Map<String, PsiClass>> buildClassCacheAndSetClassOriginalValues(List<String> allReferencedClasses, GlobalSearchScope projectScope) {
    return ReadAction.nonBlocking(() -> DumbService.getInstance(project).runReadActionInSmartMode(() -> {
        final Map<String, PsiClass> classCache = new HashMap<>();
        for (String referencedClass : allReferencedClasses) {
            final PsiClass psiClass = classCache.computeIfAbsent(referencedClass, x -> JavaFileManager.getInstance(project).findClass(x, projectScope));
            if (psiClass != null) {
                psiClass.putUserData(Keys.ORIGINAL_REFERENCE_FQN, psiClass.getQualifiedName());
                psiClass.putUserData(Keys.ORIGINAL_REFERENCE_VISIBILITY, VisibilityUtil.getAccessLevel(VisibilityUtil.getVisibilityModifier(psiClass.getModifierList())));
            }
        }
        return classCache;
    })).submit(AppExecutorUtil.getAppExecutorService());
}

results in the following stack trace:

com.intellij.openapi.project.IndexNotReadyException: Please change caller according to com.intellij.openapi.project.IndexNotReadyException documentation
	at com.intellij.openapi.project.IndexNotReadyException.create(IndexNotReadyException.java:67)
	at com.intellij.util.indexing.FileBasedIndexImpl.handleDumbMode(FileBasedIndexImpl.java:821)
	at com.intellij.util.indexing.FileBasedIndexImpl.ensureUpToDate(FileBasedIndexImpl.java:769)
	at com.intellij.psi.stubs.StubIndexEx.getContainingIds(StubIndexEx.java:358)
	at com.intellij.psi.stubs.StubIndexEx.processElements(StubIndexEx.java:168)
	at com.intellij.psi.stubs.StubIndex.getElements(StubIndex.java:102)
	at com.intellij.psi.stubs.StubIndex.getElements(StubIndex.java:90)
	at com.intellij.psi.impl.java.stubs.index.JavaFullClassNameIndex.getClasses(JavaFullClassNameIndex.java:42)
	at com.intellij.psi.impl.file.impl.JavaFileManagerImpl.doFindClasses(JavaFileManagerImpl.java:81)
	at com.intellij.psi.impl.file.impl.JavaFileManagerImpl.findClass(JavaFileManagerImpl.java:104)
	at de.sist.tph.intellij.breakingapi.ApiUsagePsiElementUserDataSetter.lambda$buildClassCacheAndSetClassOriginalValues$34(ApiUsagePsiElementUserDataSetter.java:239)
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1220)
	at de.sist.tph.intellij.breakingapi.ApiUsagePsiElementUserDataSetter.lambda$buildClassCacheAndSetClassOriginalValues$35(ApiUsagePsiElementUserDataSetter.java:239)
	at com.intellij.openapi.project.DumbService.runReadActionInSmartMode$lambda$0(DumbService.kt:77)
	at com.intellij.openapi.project.DumbService.runReadActionInSmartMode(DumbService.kt:117)
	at com.intellij.openapi.project.DumbService.runReadActionInSmartMode(DumbService.kt:77)
	at de.sist.tph.intellij.breakingapi.ApiUsagePsiElementUserDataSetter.lambda$buildClassCacheAndSetClassOriginalValues$36(ApiUsagePsiElementUserDataSetter.java:236)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$OTelMonitor.callWrapped(NonBlockingReadActionImpl.java:840)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$OTelMonitor$MonitoredComputation.call(NonBlockingReadActionImpl.java:872)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.insideReadAction(NonBlockingReadActionImpl.java:604)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$attemptComputation$4(NonBlockingReadActionImpl.java:567)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1075)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runInReadActionWithWriteActionPriority$0(ProgressIndicatorUtils.java:73)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtilService.runActionAndCancelBeforeWrite(ProgressIndicatorUtilService.java:73)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runActionAndCancelBeforeWrite(ProgressIndicatorUtils.java:128)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runWithWriteActionPriority$1(ProgressIndicatorUtils.java:111)
	at com.intellij.openapi.progress.ProgressManager.lambda$runProcess$0(ProgressManager.java:73)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$1(CoreProgressManager.java:192)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:610)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:685)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:641)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:609)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:78)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:179)
	at com.intellij.openapi.progress.ProgressManager.runProcess(ProgressManager.java:73)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runWithWriteActionPriority(ProgressIndicatorUtils.java:108)
	at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runInReadActionWithWriteActionPriority(ProgressIndicatorUtils.java:73)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.attemptComputation(NonBlockingReadActionImpl.java:567)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$transferToBgThread$1(NonBlockingReadActionImpl.java:466)
	at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$transferToBgThread$2(NonBlockingReadActionImpl.java:481)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:702)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:699)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:699)
	at java.base/java.lang.Thread.run(Thread.java:840)

I thought runReadActionInSmartMode was supposed to take care of that?

It is run like this:

DumbService.getInstance(project)
        .runWhenSmart(() ->
        buildClassCacheAndSetClassOriginalValues(allReferencedClasses, projectScope)…

 

0
2 comments

Hi Simon,

The method doc says:

Pause the current thread until dumb mode ends, and then run the read action. Indexes are guaranteed to be available inside that read action, unless this method is already called with read access allowed.

To me, it looks like the error is in wrapping the whole in the read action:

return ReadAction.nonBlocking(() -> DumbService.getInstance(project).runReadActionInSmartMode(() -> { ... }
0

Argh, thank you for the hint, Karol.

Getting around all the slow operations and EDT and background thread etc. stuff isn't trivial. The JavaDoc already helps a lot but every time I get an error because I use a slow operation when it's not allowed or try to access the index when it's not ready yet I'm stumped again.

0

Please sign in to leave a comment.