Skip to content

Commit

Permalink
fix(compiler): wrap parsing operations in read actions
Browse files Browse the repository at this point in the history
Wrap parsing operations in `runReadAction` blocks in `FrontmatterParser.kt` and `ShireRunFileAction.kt` to ensure thread safety during read operations. This change helps to prevent potential concurrency issues.
  • Loading branch information
phodal committed Jun 29, 2024
1 parent 76e1700 commit 97c8d15
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.intellij.execution.runners.ExecutionEnvironmentBuilder
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
Expand All @@ -19,6 +21,8 @@ import com.phodal.shirelang.psi.ShireFile
import com.phodal.shirelang.run.ShireConfiguration
import com.phodal.shirelang.run.ShireConfigurationType
import com.phodal.shirelang.run.ShireRunConfigurationProducer
import com.phodal.shirelang.utils.ShireCoroutineScope
import kotlinx.coroutines.launch
import org.jetbrains.annotations.NonNls

class ShireRunFileAction : DumbAwareAction() {
Expand Down Expand Up @@ -73,15 +77,18 @@ class ShireRunFileAction : DumbAwareAction() {
}

fun runFile(myProject: Project, fileName: String): Any {
// first search fileName in project
val file = runReadAction {
ShireActionStartupActivity.obtainShireFiles(myProject).find {
it.name == fileName
}
} ?: return "File not found"

val config = DynamicShireActionConfig.from(file)
return executeShireFile(myProject, config, null)
ApplicationManager.getApplication().invokeLater({
val config = DynamicShireActionConfig.from(file)
executeShireFile(myProject, config, null)
}, ModalityState.NON_MODAL)

return ""
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.phodal.shirelang.compiler

import com.intellij.openapi.application.invokeAndWaitIfNeeded
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.logger
import com.intellij.psi.PsiElement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.phodal.shirelang.compiler.hobbit.execute

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeAndWaitIfNeeded
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.intellij.execution.runners.showRunContent
import com.intellij.execution.ui.RunContentDescriptor
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileDocumentManager
import java.util.concurrent.atomic.AtomicReference

class ShireProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {
Expand All @@ -29,7 +28,8 @@ class ShireProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {
if (environment.runProfile !is ShireConfiguration) return null
val shireState = state as ShireRunConfigurationProfileState

FileDocumentManager.getInstance().saveAllDocuments()
// FileDocumentManager.getInstance().saveAllDocuments()

val result = AtomicReference<RunContentDescriptor>()

if(!isSubscribed) {
Expand All @@ -38,6 +38,7 @@ class ShireProgramRunner : GenericProgramRunner<RunnerSettings>(), Disposable {
val consoleView = (environment.state as? ShireRunConfigurationProfileState)?.console
environment.project.getService(ShireProcessProcessor::class.java)
.process(string, event, scriptPath, consoleView)

}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.execution.process.ProcessEvent
import com.intellij.execution.process.ProcessTerminatedListener
import com.intellij.execution.runners.ProgramRunner
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.application.ApplicationManager
Expand Down Expand Up @@ -38,7 +39,7 @@ import javax.swing.JComponent
open class ShireRunConfigurationProfileState(
private val myProject: Project,
private val configuration: ShireConfiguration,
) : RunProfileState {
) : RunProfileState, Disposable {
private var executionConsole: ConsoleViewImpl? = null
var console: ShireConsoleView? = null

Expand Down Expand Up @@ -140,6 +141,11 @@ open class ShireRunConfigurationProfileState(

console.print("\n--------------------\n", ConsoleViewContentType.NORMAL_OUTPUT)
}

override fun dispose() {
console?.dispose()
executionConsole?.dispose()
}
}

class ShireConsoleView(private val executionConsole: ConsoleViewImpl) :
Expand All @@ -156,6 +162,11 @@ class ShireConsoleView(private val executionConsole: ConsoleViewImpl) :
toolbar.targetComponent = baseComponent
myPanel.add(toolbar.component, BorderLayout.EAST)
}

override fun dispose() {
super.dispose()
executionConsole.dispose()
}
}

class ShireProcessAdapter(private val sb: StringBuilder, val configuration: ShireConfiguration) : ProcessAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.execution.console.ConsoleViewWrapperBase
import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.project.Project
import com.phodal.shire.llm.LlmProvider
import com.phodal.shirelang.ShireBundle
Expand All @@ -22,7 +23,7 @@ class ShireDefaultRunner(
private val isLocalMode: Boolean,
) : ShireRunner(configuration, processHandler, console, myProject, prompt) {
override fun execute(postFunction: (response: String) -> Unit) {
ApplicationManager.getApplication().invokeLater {
ApplicationManager.getApplication().invokeLater({
if (isLocalMode) {
console.print(ShireBundle.message("shire.run.local.mode"), ConsoleViewContentType.SYSTEM_OUTPUT)
processHandler.detachProcess()
Expand All @@ -48,7 +49,7 @@ class ShireDefaultRunner(
postFunction(response)
processHandler.detachProcess()
}
}
}, ModalityState.NON_MODAL)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ShireLifecycleTest : BasePlatformTestCase() {
}

fun testShouldHandleWhenAfterStreaming() {

val code2 = "hi"
val code = """
---
afterStreaming: {
Expand All @@ -46,7 +48,7 @@ class ShireLifecycleTest : BasePlatformTestCase() {
case condition {
"error" { notify("Failed to Generate JSON") }
"success" { notify("Success to Generate JSON") }
"json-result" { execute("sample.shire") }
"json-result" { execute("sample2.shire") }
default { notify("Failed to Generate JSON") /* mean nothing */ }
}
}
Expand All @@ -55,6 +57,7 @@ class ShireLifecycleTest : BasePlatformTestCase() {
${'$'}allController
""".trimIndent()

myFixture.addFileToProject("sample2.shire", code2)
val file = myFixture.addFileToProject("sample.shire", code)

myFixture.openFileInEditor(file.virtualFile)
Expand Down

0 comments on commit 97c8d15

Please sign in to comment.