java.lang.Throwable: Synchronous execution under ReadAction
Answered
Hello,
I need to get file content by sha in my LineMarkerProviderDescriptor. But I'm getting "java.lang.Throwable: Synchronous execution under ReadAction".
Right now my code looks like this:
private fun getFileContentByCommitSha(commitSha: String): String? {
val completableFuture = CompletableFuture<String>()
ApplicationManager.getApplication().executeOnPooledThread {
val str = ApplicationManager.getApplication().runReadAction<String> {
val git = Git.getInstance()
val repositoryManager = GitRepositoryManager.getInstance(project)
val repository = repositoryManager.repositories.first()
val result = git.show(repository, "$commitSha:$filePath")
result.outputAsJoinedString
}
completableFuture.complete(str)
}
return completableFuture.get()
}
Could someone help?
Please sign in to leave a comment.
To resolve this issue, you can modify your code to avoid the synchronous execution within the runReadAction block. Here's an updated version of your code:
private fun getFileContentByCommitSha(commitSha: String): String? {
val completableFuture = CompletableFuture<String>()
ApplicationManager.getApplication().executeOnPooledThread {
val str = ApplicationManager.getApplication().runReadAction<String> {
val git = Git.getInstance()
val repositoryManager = GitRepositoryManager.getInstance(project)
val repository = repositoryManager.repositories.first()
val result = git.show(repository, "$commitSha:$filePath")
result.outputAsJoinedString
}
ApplicationManager.getApplication().invokeLater {
completableFuture.complete(str)
}
}
return completableFuture.get()
}
In this updated code, instead of calling completableFuture.complete(str) directly within the runReadAction block, we use ApplicationManager.getApplication().invokeLater to complete the completableFuture in the UI thread after the read action is executed.
Thank you! I'll try!
Hi Alexandr,
The previous answer seems like generated with ChatGPT and doesn't look good. It also contained a spam link, so I would ignore it.
Could you please provide more context? What is the stacktrace? What is the full
LineMarkerProviderDescriptor
implementation? Why do you execute your code inApplication.executeOnPooledThread()
? Did you consider implementingLineMarkerProviderDescriptor.collectSlowLineMarkers()
?