How to use LocalFileSystem.getInstance().refreshAndFindFileByNIO() method in a thread where only read allowed?

Answered

I'm trying to find a file in SDK after the SDK is downloaded. However, I cant get file using the LocalFileSystem.getInstance().FindFileByNioFile(path) method cause the LocalFileSystem is not latest. Next ,I  try to use refreshAndFindFileByNioFile(path) method in  a writeAction, 

ApplicationManager.getApplication()
.runWriteAction((Computable<VirtualFile>) () -> lfs.refreshAndFindFileByNioFile(path));

but end up get error message, 

java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. Please ensure you're using invokeLater/invokeAndWait with a correct modality state (not "any"). See TransactionGuard documentation for details.
  current modality=ModalityState.NON_MODAL
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:182)
    at com.intellij.openapi.application.TransactionGuardImpl.assertWriteActionAllowed(TransactionGuardImpl.java:140)
    at com.intellij.openapi.vfs.newvfs.RefreshQueueImpl.queueSessionSync(RefreshQueueImpl.java:68)
    at com.intellij.openapi.vfs.newvfs.RefreshQueueImpl.execute(RefreshQueueImpl.java:54)
    at com.intellij.openapi.vfs.newvfs.RefreshSessionImpl.launch(RefreshSessionImpl.java:115)
    at com.intellij.openapi.vfs.newvfs.RefreshQueueImpl.processSingleEvent(RefreshQueueImpl.java:200)
    at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.createAndFindChildWithEventFire(VirtualDirectoryImpl.java:309)
    at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.findChild(VirtualDirectoryImpl.java:82)
    at com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl.refreshAndFindChild(VirtualDirectoryImpl.java:346)
    at com.intellij.openapi.vfs.newvfs.VfsImplUtil.refreshAndFindFileByPath(VfsImplUtil.java:121)
    at com.intellij.openapi.vfs.impl.local.LocalFileSystemBase.refreshAndFindFileByPath(LocalFileSystemBase.java:69)
    at com.intellij.openapi.vfs.LocalFileSystem.refreshAndFindFileByNioFile(LocalFileSystem.java:51)

 

So ,how can I refresh LocalFileSystem  instance in a thread where only read allowed? 

Thank you!

 

7 comments
Comment actions Permalink

Please show full stacktrace, not arbitrarily cut-off one.

0
Comment actions Permalink

@Yann Cebron ,Thank you for answering this, the whole error message is that

Write access is allowed from write thread only
com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: EventQueue.isDispatchThread()=false Toolkit.getEventQueue()=com.intellij.ide.IdeEventQueue@3f5ddaff
Current thread: Thread[ApplicationImpl pooled thread 1,4,main] 307895593
Write thread: Thread[AWT-EventQueue-0,6,main] 631966621
    at com.intellij.openapi.application.impl.ApplicationImpl.assertIsWriteThread(ApplicationImpl.java:1041)
    at com.intellij.openapi.application.impl.ApplicationImpl.startWrite(ApplicationImpl.java:1123)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteActionWithClass(ApplicationImpl.java:922)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:945)
  at com.example.myplugin.MyJsonSchemaFileProvider.getSchemaFile(MyJsonSchemaFileProvider.java:89)
  at com.example.myplugin.MyJsonSchemaProvider.getSchemaFile(MyJsonSchemaProvider.java:33)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl.getSchemaForProvider(JsonSchemaServiceImpl.java:592)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl$MyState.createFileProviderMap(JsonSchemaServiceImpl.java:566)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl$MyState$1.compute(JsonSchemaServiceImpl.java:499)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl$MyState$1.compute(JsonSchemaServiceImpl.java:495)
    at com.intellij.openapi.util.ClearableLazyValue.getValue(ClearableLazyValue.java:39)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl$MyState$1.getValue(JsonSchemaServiceImpl.java:507)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl$MyState$1.getValue(JsonSchemaServiceImpl.java:495)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl$MyState.getFiles(JsonSchemaServiceImpl.java:535)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl.isMappedSchema(JsonSchemaServiceImpl.java:369)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl.isMappedSchema(JsonSchemaServiceImpl.java:365)
    at com.jetbrains.jsonSchema.impl.JsonSchemaServiceImpl.isSchemaFile(JsonSchemaServiceImpl.java:353)
    at com.jetbrains.jsonSchema.JsonSchemaIconProvider.getIcon(JsonSchemaIconProvider.java:25)
    at com.intellij.util.IconUtil.getProvidersIcon(IconUtil.java:222)
    at com.intellij.util.IconUtil.computeFileIcon(IconUtil.java:155)
    at com.intellij.openapi.fileEditor.impl.EditorsSplitters.lambda$updateFileIconAsynchronously$7(EditorsSplitters.java:430)
    at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.insideReadAction(NonBlockingReadActionImpl.java:521)
    at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$attemptComputation$3(NonBlockingReadActionImpl.java:486)
    at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1078)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runInReadActionWithWriteActionPriority$0(ProgressIndicatorUtils.java:78)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runActionAndCancelBeforeWrite(ProgressIndicatorUtils.java:161)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.lambda$runWithWriteActionPriority$1(ProgressIndicatorUtils.java:118)
    at com.intellij.openapi.progress.ProgressManager.lambda$runProcess$0(ProgressManager.java:57)
    at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:183)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:705)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:647)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:63)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:170)
    at com.intellij.openapi.progress.ProgressManager.runProcess(ProgressManager.java:57)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runWithWriteActionPriority(ProgressIndicatorUtils.java:115)
    at com.intellij.openapi.progress.util.ProgressIndicatorUtils.runInReadActionWithWriteActionPriority(ProgressIndicatorUtils.java:78)
    at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.attemptComputation(NonBlockingReadActionImpl.java:486)
    at com.intellij.openapi.application.impl.NonBlockingReadActionImpl$Submission.lambda$transferToBgThread$1(NonBlockingReadActionImpl.java:407)
    at com.intellij.util.concurrency.BoundedTaskExecutor.doRun(BoundedTaskExecutor.java:216)
    at com.intellij.util.concurrency.BoundedTaskExecutor.access$200(BoundedTaskExecutor.java:27)
    at com.intellij.util.concurrency.BoundedTaskExecutor$1.execute(BoundedTaskExecutor.java:195)
    at com.intellij.util.concurrency.BoundedTaskExecutor$1.run(BoundedTaskExecutor.java:187)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:668)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:665)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:665)
    at java.base/java.lang.Thread.run(Thread.java:829)

MyJsonSchemaFileProvider is my custom schemaFileProvider implemention.

 

0
Comment actions Permalink

Why do you need to lookup that file in filesystem? Why on every single call?

0
Comment actions Permalink

@Yann Cebron,In my situation, the schema file of config file changes  under certain conditions. So I just look up fileSysytem when searching for schema file in method getSchemaFile(), But when this method is called, the thread state always only allow read , So I cant refresh fileSystem to get the latest schema file. 

0
Comment actions Permalink

Where is the schema file actually located physically?

0
Comment actions Permalink

@Yann Cebron, the schema file is under my user directory C:\Users\yaphets123\AppData\Local\myshema.json. This file may not esixts, but when I copy one to this directory, LocalFileSystem cant sense the existence of the schema file, So I'm trying to refresh the localFileSystem, but got error msg presented above.

1
Comment actions Permalink

Please try using com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory#getResourceFile it should accept absolute URL (see com.intellij.openapi.vfs.VfsUtil#findFileByURL)

0

Please sign in to leave a comment.