IllegalStateException: read job must've been cancelled" when Creating PHP Files in PhpStorm
已回答
My plugin creates PHP files through actions, but sometimes I get errors when creating and opening the created file in PhpStorm.
Has anyone else experienced this or have any suggestions for troubleshooting?
package at.myplugin.actions;
import at.myplugin.actions.views.ViewFileView;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;
public class ViewAction extends BaseAction {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
ViewFileView viewFileView = new ViewFileView(anActionEvent.getProject());
if (viewFileView.showAndGet()) {
this.create(
viewFileView.getViewFileModel(),
"view.ftl",
true,
anActionEvent.getProject()
);
}
}
/**
* Creates a file from a template
* @param model The data model for the template.
* @param templateName The name of the template file.
* @param openInEditor Whether to open the created file in the editor.
* @param project The current project.
*/
protected void create(BaseModel model, String templateName, boolean openInEditor, Project project) {
TemplateLoader templateProcessor = new TemplateLoader(
project,
templateName,
model
);
templateProcessor.createTemplateWithDirectory(openInEditor);
}
}
package at.myplugin.support.template;
import at.myplugin.actions.models.BaseModel;
import at.myplugin.support.notification.Notify;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* Loads and processes Freemarker templates
* It creates php files
*/
public class TemplateLoader {
/**
* The current project
*/
private Project project;
/**
* The Freemarker configuration
*/
private Configuration configuration;
/**
* The name of the freemaker template file
*/
private final String template;
/**
* The data model for the template
*/
private final BaseModel object;
/**
* @param project The current project
* @param template The name of the template file
* @param object The data model for the template
*/
public TemplateLoader(Project project, String template, BaseModel object) {
this.project = project;
this.template = template;
this.object = object;
}
/**
* Creates the template file and its directory if it doesn't exist.
* Opens the file in the IDE if needed
*
* @param openInEditor Whether to open the created file in the editor
*/
public void createTemplateWithDirectory(boolean openInEditor) {
ApplicationManager.getApplication().invokeLater(() -> {
WriteCommandAction.runWriteCommandAction(project, () -> {
try {
VirtualFile virtualFile = VfsUtil.createDirectories(getDirectoryWithoutSlash());
createTemplateOnly();
virtualFile.refresh(false, false);
if (openInEditor) {
this.openFileInEditor(virtualFile);
}
} catch (IOException e) {
Notify.notifyUser(
this.project,
"Could not create " + this.object.getName(),
NotificationType.ERROR
);
}
});
});
}
/**
* Creates the php file from the template
*/
public void createTemplateOnly() {
try {
Writer writer = new FileWriter(
this.project.getBasePath()
+ this.object.getDestination()
+ this.object.getName()
+ this.object.getExtension()
);
this.configuration = new Configuration(Configuration.VERSION_2_3_32);
this.configuration.setTemplateLoader(
new ClassTemplateLoader(getClass().getClassLoader(), "/templates")
);
this.configuration.setDefaultEncoding("UTF-8");
this.configuration.getTemplate(this.template).process(this.object, writer);
writer.close();
Notify.notifyUser(
this.project,
this.object.getName() + " created successfully",
NotificationType.INFORMATION
);
} catch (IOException | TemplateException ex) {
Notify.notifyUser(
this.project,
"Could not create " + this.object.getName(),
NotificationType.ERROR
);
}
}
/**
* Opens the created file in the editor
* @param virtualFile The folder where the target file lives
*/
public void openFileInEditor(VirtualFile virtualFile) {
PsiDirectory targetDir = PsiManager.getInstance(project).findDirectory(virtualFile);
PsiFile psiFile = null;
if (targetDir != null) {
psiFile = targetDir.findFile(
this.object.getName()
+ this.object.getExtension()
);
}
if (psiFile != null) {
OpenFileDescriptor descriptor = new OpenFileDescriptor(this.project, psiFile.getVirtualFile());
FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
}
}
/**
* Returns the directory path without a trailing slash
* @return The directory path
*/
private String getDirectoryWithoutSlash() {
String str = project.getBasePath()
+ this.object.getDestination()
+ this.object.getFolderPath();
if (str.endsWith("/")) {
str = str.substring(0, str.length() - 1);
}
return str;
}
}
2024-10-06 13:02:15,686 [ 31987] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - Unhandled exception in [ComponentManager(ProjectImpl@395897499), CoroutineName(com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProviderService), Dispatchers.Default]
java.lang.IllegalStateException: read job must've been cancelled
at com.intellij.openapi.application.rw.CancellableReadActionKt.cancellableReadActionInternal(cancellableReadAction.kt:37)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadCancellable(InternalReadAction.kt:93)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadAction(InternalReadAction.kt:77)
at com.intellij.openapi.application.rw.InternalReadAction.readLoop(InternalReadAction.kt:64)
at com.intellij.openapi.application.rw.InternalReadAction.access$readLoop(InternalReadAction.kt:15)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invokeSuspend(InternalReadAction.kt:43)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:62)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:163)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
at com.intellij.openapi.application.rw.InternalReadAction.runReadAction(InternalReadAction.kt:39)
at com.intellij.openapi.application.rw.PlatformReadWriteActionSupport.executeReadAction(PlatformReadWriteActionSupport.kt:38)
at com.intellij.openapi.application.ReadWriteActionSupport.executeReadAction$default(ReadWriteActionSupport.kt:15)
at com.intellij.openapi.application.CoroutinesKt.constrainedReadAction(coroutines.kt:58)
at com.intellij.openapi.application.CoroutinesKt.readAction(coroutines.kt:25)
at com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProvider$AsyncComputation$start$2.invokeSuspend(CodeStyleCachedValueProvider.kt:135)
at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
at com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProvider$AsyncComputation$start$2.invokeSuspend(CodeStyleCachedValueProvider.kt:135)
Caused by: java.lang.IllegalStateException: read job must've been cancelled
at com.intellij.openapi.application.rw.CancellableReadActionKt.cancellableReadActionInternal(cancellableReadAction.kt:37)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadCancellable(InternalReadAction.kt:93)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadAction(InternalReadAction.kt:77)
at com.intellij.openapi.application.rw.InternalReadAction.readLoop(InternalReadAction.kt:64)
at com.intellij.openapi.application.rw.InternalReadAction.access$readLoop(InternalReadAction.kt:15)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invokeSuspend(InternalReadAction.kt:43)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:62)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:163)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
at com.intellij.openapi.application.rw.InternalReadAction.runReadAction(InternalReadAction.kt:39)
at com.intellij.openapi.application.rw.PlatformReadWriteActionSupport.executeReadAction(PlatformReadWriteActionSupport.kt:38)
at com.intellij.openapi.application.ReadWriteActionSupport.executeReadAction$default(ReadWriteActionSupport.kt:15)
at com.intellij.openapi.application.CoroutinesKt.constrainedReadAction(coroutines.kt:58)
at com.intellij.openapi.application.CoroutinesKt.readAction(coroutines.kt:25)
at com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProvider$AsyncComputation$start$2.invokeSuspend(CodeStyleCachedValueProvider.kt:135)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:608)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:873)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:763)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:750)
Exception in thread "DefaultDispatcher-worker-7 @com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProviderService#17151" java.lang.IllegalStateException: read job must've been cancelled
at com.intellij.openapi.application.rw.CancellableReadActionKt.cancellableReadActionInternal(cancellableReadAction.kt:37)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadCancellable(InternalReadAction.kt:93)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadAction(InternalReadAction.kt:77)
at com.intellij.openapi.application.rw.InternalReadAction.readLoop(InternalReadAction.kt:64)
at com.intellij.openapi.application.rw.InternalReadAction.access$readLoop(InternalReadAction.kt:15)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invokeSuspend(InternalReadAction.kt:43)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:62)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:163)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
at com.intellij.openapi.application.rw.InternalReadAction.runReadAction(InternalReadAction.kt:39)
at com.intellij.openapi.application.rw.PlatformReadWriteActionSupport.executeReadAction(PlatformReadWriteActionSupport.kt:38)
at com.intellij.openapi.application.ReadWriteActionSupport.executeReadAction$default(ReadWriteActionSupport.kt:15)
at com.intellij.openapi.application.CoroutinesKt.constrainedReadAction(coroutines.kt:58)
at com.intellij.openapi.application.CoroutinesKt.readAction(coroutines.kt:25)
at com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProvider$AsyncComputation$start$2.invokeSuspend(CodeStyleCachedValueProvider.kt:135)
at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
at com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProvider$AsyncComputation$start$2.invokeSuspend(CodeStyleCachedValueProvider.kt:135)
Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [ComponentManager(ProjectImpl@395897499), CoroutineName(com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProviderService), CoroutineId(17151), "com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProviderService#17151":StandaloneCoroutine{Cancelling}@d4eec0e, Dispatchers.Default]
Caused by: java.lang.IllegalStateException: read job must've been cancelled
at com.intellij.openapi.application.rw.CancellableReadActionKt.cancellableReadActionInternal(cancellableReadAction.kt:37)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadCancellable(InternalReadAction.kt:93)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadAction(InternalReadAction.kt:77)
at com.intellij.openapi.application.rw.InternalReadAction.readLoop(InternalReadAction.kt:64)
at com.intellij.openapi.application.rw.InternalReadAction.access$readLoop(InternalReadAction.kt:15)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invokeSuspend(InternalReadAction.kt:43)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:62)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:163)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
at com.intellij.openapi.application.rw.InternalReadAction.runReadAction(InternalReadAction.kt:39)
at com.intellij.openapi.application.rw.PlatformReadWriteActionSupport.executeReadAction(PlatformReadWriteActionSupport.kt:38)
at com.intellij.openapi.application.ReadWriteActionSupport.executeReadAction$default(ReadWriteActionSupport.kt:15)
at com.intellij.openapi.application.CoroutinesKt.constrainedReadAction(coroutines.kt:58)
at com.intellij.openapi.application.CoroutinesKt.readAction(coroutines.kt:25)
at com.intellij.application.options.codeStyle.cache.CodeStyleCachedValueProvider$AsyncComputation$start$2.invokeSuspend(CodeStyleCachedValueProvider.kt:135)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:608)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:873)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:763)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:750)
2024-10-06 13:02:15,691 [ 31992] WARN - #c.i.o.f.AsyncFileEditorProvider - Exception during editor loading
java.lang.IllegalStateException: read job must've been cancelled
at com.intellij.openapi.application.rw.CancellableReadActionKt.cancellableReadActionInternal(cancellableReadAction.kt:37)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadCancellable(InternalReadAction.kt:93)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadAction(InternalReadAction.kt:77)
at com.intellij.openapi.application.rw.InternalReadAction.readLoop(InternalReadAction.kt:64)
at com.intellij.openapi.application.rw.InternalReadAction.access$readLoop(InternalReadAction.kt:15)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invokeSuspend(InternalReadAction.kt:43)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:62)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:163)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
at com.intellij.openapi.application.rw.InternalReadAction.runReadAction(InternalReadAction.kt:39)
at com.intellij.openapi.application.rw.PlatformReadWriteActionSupport.executeReadAction(PlatformReadWriteActionSupport.kt:38)
at com.intellij.openapi.application.ReadWriteActionSupport.executeReadAction$default(ReadWriteActionSupport.kt:15)
at com.intellij.openapi.application.CoroutinesKt.constrainedReadAction(coroutines.kt:58)
at com.intellij.openapi.application.CoroutinesKt.readAction(coroutines.kt:25)
at com.intellij.openapi.fileEditor.impl.text.FoldingTextEditorInitializer.initializeEditor(FoldingTextEditorInitializer.kt:31)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorProvider$createInitTask$1$1$1.invokeSuspend(PsiAwareTextEditorProvider.kt:138)
at _COROUTINE._BOUNDARY._(CoroutineDebugging.kt:42)
at com.intellij.openapi.fileEditor.impl.text.FoldingTextEditorInitializer.initializeEditor(FoldingTextEditorInitializer.kt:31)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorProvider$createInitTask$1$1$1.invokeSuspend(PsiAwareTextEditorProvider.kt:138)
Caused by: java.lang.IllegalStateException: read job must've been cancelled
at com.intellij.openapi.application.rw.CancellableReadActionKt.cancellableReadActionInternal(cancellableReadAction.kt:37)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadCancellable(InternalReadAction.kt:93)
at com.intellij.openapi.application.rw.InternalReadAction.tryReadAction(InternalReadAction.kt:77)
at com.intellij.openapi.application.rw.InternalReadAction.readLoop(InternalReadAction.kt:64)
at com.intellij.openapi.application.rw.InternalReadAction.access$readLoop(InternalReadAction.kt:15)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invokeSuspend(InternalReadAction.kt:43)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at com.intellij.openapi.application.rw.InternalReadAction$runReadAction$4.invoke(InternalReadAction.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:62)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:163)
at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
at com.intellij.openapi.application.rw.InternalReadAction.runReadAction(InternalReadAction.kt:39)
at com.intellij.openapi.application.rw.PlatformReadWriteActionSupport.executeReadAction(PlatformReadWriteActionSupport.kt:38)
at com.intellij.openapi.application.ReadWriteActionSupport.executeReadAction$default(ReadWriteActionSupport.kt:15)
at com.intellij.openapi.application.CoroutinesKt.constrainedReadAction(coroutines.kt:58)
at com.intellij.openapi.application.CoroutinesKt.readAction(coroutines.kt:25)
at com.intellij.openapi.fileEditor.impl.text.FoldingTextEditorInitializer.initializeEditor(FoldingTextEditorInitializer.kt:31)
at com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorProvider$createInitTask$1$1$1.invokeSuspend(PsiAwareTextEditorProvider.kt:138)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:608)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:873)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:763)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:750)
请先登录再写评论。
Hi,
I don't see any issue with your code. Does it report an issue for your plugin or PhpStorm? If for PhpStorm (I guess it is, because there is no your code in the stacktrace), please report them from the error report dialog.
Karol Lewandowski I get this error when I create and open multiple files at the same time(after each other).
I could not figure out what is wrong.