Skip to content

Commit

Permalink
KTIJ-19119 [Kotlin Scratch]: synchronous execution under ReadAction
Browse files Browse the repository at this point in the history
This commit fixes a problem reported as "Synchronous execution
under ReadAction".

KtScratchExecutionSession.runCommandLine() launched under ReadAction
fails from time to time whatever declared execution context is
(sync or async).

Since read-lock isn't explicitly required for the call (detected
neither in theory nor at runtime), its scope was limited.

GitOrigin-RevId: 46688cb3f5212838693713a6e823c81d49cf279c
  • Loading branch information
klunnii authored and intellij-monorepo-bot committed Jul 21, 2021
1 parent b1a639f commit 5127a5b
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Computable
import com.intellij.openapi.util.io.FileUtil
import com.intellij.util.concurrency.AppExecutorUtil
import com.intellij.util.concurrency.NonUrgentExecutor
import org.jetbrains.kotlin.idea.KotlinJvmBundle
import org.jetbrains.kotlin.idea.core.KotlinCompilerIde
import org.jetbrains.kotlin.idea.core.script.ScriptConfigurationManager
Expand Down Expand Up @@ -60,26 +62,22 @@ class KtScratchExecutionSession(
override fun run(indicator: ProgressIndicator) {
backgroundProcessIndicator = indicator

ReadAction.nonBlocking {
val modifiedScratchSourceFile =
KtPsiFactory(psiFile.project).createFileWithLightClassSupport("tmp.kt", result.code, psiFile)
try {
runCommandLine(project, modifiedScratchSourceFile, expressions, psiFile, result, indicator, callback)
} catch (e: Throwable) {
if (e is ControlFlowException) throw e

LOG.printDebugMessage(result.code)
executor.errorOccurs(
e.message ?: KotlinJvmBundle.message("couldn.t.compile.0", psiFile.name),
e,
isFatal = true
)
}
val modifiedScratchSourceFile = runReadAction {
KtPsiFactory(psiFile.project).createFileWithLightClassSupport("tmp.kt", result.code, psiFile)
}

try {
runCommandLine(project, modifiedScratchSourceFile, expressions, psiFile, result, indicator, callback)
} catch (e: Throwable) {
if (e is ControlFlowException) throw e

LOG.printDebugMessage(result.code)
executor.errorOccurs(
e.message ?: KotlinJvmBundle.message("couldn.t.compile.0", psiFile.name),
e,
isFatal = true
)
}
.inSmartMode(project)
.wrapProgress(indicator)
.withDocumentsCommitted(project)
.executeSynchronously()
}
}.queue()
}
Expand Down

0 comments on commit 5127a5b

Please sign in to comment.