Skip to content

Commit 7bca4d7

Browse files
committed
fix: guard PSI/module resolution with read lock in notifyElementModifiedForPath
psiManager.findFile / structureProvider.getModule / findModuleForSourceId ran without holding the read lock, so they could race a concurrent `analyze` read section. Resolve them inside project.read { } (mirroring loadKtFile) and hand the results to the existing project.write { } mutation block.
1 parent 90e5471 commit 7bca4d7

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

‎lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/CompilationEnvironment.kt‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,20 @@ internal class CompilationEnvironment(
210210
path: Path,
211211
crossinline typeProvider: (KtFile) -> KaElementModificationType,
212212
) {
213-
val structureProvider = ProjectStructureProvider.getInstance(project)
214-
val ktFile = path.toVirtualFileOrNull()?.let {
215-
psiManager.findFile(it) as? KtFile
216-
}
213+
// Resolve PSI/module structure under the read lock, mirroring loadKtFile(); driving
214+
// psiManager.findFile / structureProvider concurrently with an `analyze` read section
215+
// otherwise races.
216+
val (ktFile, module) = project.read {
217+
val structureProvider = ProjectStructureProvider.getInstance(project)
218+
val ktFile = path.toVirtualFileOrNull()?.let {
219+
psiManager.findFile(it) as? KtFile
220+
}
217221

218-
val module = (ktFile?.let { structureProvider.getModule(it, null) }
219-
?: structureProvider.findModuleForSourceId(path.pathString)) as? AbstractKtModule
222+
val module = (ktFile?.let { structureProvider.getModule(it, null) }
223+
?: structureProvider.findModuleForSourceId(path.pathString)) as? AbstractKtModule
224+
225+
ktFile to module
226+
}
220227

221228
project.write {
222229
// Must run under the write lock so the session mutation can't race a concurrent

0 commit comments

Comments
 (0)