getappmap / appmap-intellij-plugin

MIT License
38 stars 9 forks source link

`com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: Read access is allowed from inside read-action or Event Dispatch Thread (EDT) only` when running with AppMap #666

Closed ahtrotta closed 5 months ago

ahtrotta commented 7 months ago

Image

In petclinic using 2023.2 this error occurs when using Run with AppMap:

com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: Read access is allowed from inside read-action or Event Dispatch Thread (EDT) only (see Application.runReadAction()); see https://jb.gg/ij-platform-threading for details
Current thread: Thread[ApplicationImpl pooled thread 30,4,main] 218926908 (EventQueue.isDispatchThread()=false)
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,main] 1560240559
    at com.intellij.openapi.application.impl.ApplicationImpl.createThreadAccessException(ApplicationImpl.java:1083)
    at com.intellij.openapi.application.impl.ApplicationImpl.assertReadAccessAllowed(ApplicationImpl.java:1038)
    at com.intellij.workspaceModel.core.fileIndex.impl.WorkspaceFileIndexDataImpl.ensureIsUpToDate(WorkspaceFileIndexDataImpl.kt:132)
    at com.intellij.workspaceModel.core.fileIndex.impl.WorkspaceFileIndexDataImpl.getFileInfo(WorkspaceFileIndexDataImpl.kt:74)
    at com.intellij.workspaceModel.core.fileIndex.impl.WorkspaceFileIndexImpl.getFileInfo(WorkspaceFileIndexImpl.kt:220)
    at com.intellij.workspaceModel.core.fileIndex.impl.WorkspaceFileIndexImpl.findFileSetWithCustomData(WorkspaceFileIndexImpl.kt:205)
    at com.intellij.openapi.roots.impl.ProjectFileIndexImpl.getModuleForFile(ProjectFileIndexImpl.java:97)
    at com.intellij.openapi.roots.impl.ProjectFileIndexImpl.getModuleForFile(ProjectFileIndexImpl.java:90)
    at org.jetbrains.plugins.gradle.execution.build.GradleBaseApplicationEnvironmentProvider.createExecutionEnvironment(GradleBaseApplicationEnvironmentProvider.kt:74)
    at org.jetbrains.plugins.gradle.execution.build.GradleProjectTaskRunner.createExecutionEnvironment(GradleProjectTaskRunner.java:308)
    at com.intellij.task.impl.ExecutionEnvironmentProviderImpl.createExecutionEnvironment$lambda$0(ExecutionEnvironmentProviderImpl.kt:33)
    at com.intellij.openapi.extensions.impl.ExtensionProcessingHelper.computeSafeIfAny(ExtensionProcessingHelper.kt:57)
    at com.intellij.openapi.extensions.ExtensionPointName.computeSafeIfAny(ExtensionPointName.kt:57)
    at com.intellij.task.impl.ExecutionEnvironmentProviderImpl.createExecutionEnvironment(ExecutionEnvironmentProviderImpl.kt:30)
    at com.intellij.execution.runners.ExecutionEnvironmentBuilder.build(ExecutionEnvironmentBuilder.kt:150)
    at com.intellij.execution.runners.ExecutionEnvironmentBuilder.build$default(ExecutionEnvironmentBuilder.kt:146)
    at com.intellij.execution.impl.ExecutionManagerImpl.compileAndRun$lambda$5(ExecutionManagerImpl.kt:403)
    at com.intellij.openapi.application.impl.ApplicationImpl$2.run(ApplicationImpl.java:272)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    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:833)
jansorg commented 5 months ago

I debugged this and now think that it's not possible to fix this in our plugin.

"Run with AppMap" on 2023.1 and 2023.2 causes this exception. The exception is thrown in code of the IDE's Gradle plugin when our "Run with AppMap" runner triggers the execution after preparing the JVM commandline.

    at com.intellij.openapi.roots.impl.ProjectFileIndexImpl.getModuleForFile(ProjectFileIndexImpl.java:90)
    at org.jetbrains.plugins.gradle.execution.build.GradleBaseApplicationEnvironmentProvider.createExecutionEnvironment(GradleBaseApplicationEnvironmentProvider.kt:74)

Method getModuleForFile is called without a ReadAction.

The code is executed async by the IDE's code and I don't see a way to hook into this to avoid the exception.

Possible alternative

The API of run configurations of the SDK is very complex and mostly undocumented. There's a GradleRunConfigurationProducer which seems suitable to replace our runner for Gradle-based projects. But it's currently unclear if this could be used because it's seems to be unavailable in the JetBrains Gradle plugin distribution.

jansorg commented 5 months ago

@dustinbyrne JetBrains replied that it's not possible to use GradleRunConfigurationProducer, because it's packaged for internal-use only (which is unusual, to say the least). That rules out the alternative which doesn't allow us to fix the exception in the AppMap plugin.