User: rednesto Date: 08 Feb 24 16:15 Revision: c90435bcde5562d227c14313fdf116a92cd05f4c Summary: Attempt to fix some CME during facet detection TeamCity URL: https://ci.mcdev.io/viewModification.html?tab=vcsModificationFiles&modId=9123&personal=false Index: src/main/kotlin/facet/MinecraftFacetDetector.kt =================================================================== --- src/main/kotlin/facet/MinecraftFacetDetector.kt (revision 181b30d36a8b952f7d5950fa73c85812a8caba2c) +++ src/main/kotlin/facet/MinecraftFacetDetector.kt (revision c90435bcde5562d227c14313fdf116a92cd05f4c) @@ -50,12 +50,10 @@ import com.intellij.openapi.util.Key import com.intellij.platform.ide.progress.withBackgroundProgress import com.intellij.platform.util.progress.forEachWithProgress -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlinx.coroutines.cancelAndJoin -import kotlinx.coroutines.job -import kotlinx.coroutines.launch +import com.intellij.util.concurrency.NonUrgentExecutor +import kotlinx.coroutines.* +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import org.jetbrains.plugins.gradle.util.GradleUtil class MinecraftFacetDetector : ProjectActivity { @@ -69,15 +67,13 @@ override suspend fun execute(project: Project) { val detectorService = project.service() - detectorService.currentJob?.cancelAndJoin() - withBackgroundProgress(project, "Detecting Minecraft Frameworks", cancellable = false) { - detectorService.currentJob = coroutineContext.job - MinecraftModuleRootListener.doCheck(project) + MinecraftModuleRootListener.doCheckUnderProgress(project, detectorService) - } + } - } @Service(Service.Level.PROJECT) private class FacetDetectorScopeProvider(val scope: CoroutineScope) { + val dispatcher = NonUrgentExecutor.getInstance().asCoroutineDispatcher() + val lock = Mutex() var currentJob: Job? = null } @@ -89,14 +85,20 @@ val project = event.source as? Project ?: return val detectorService = project.service() - detectorService.scope.launch { - detectorService.currentJob?.cancelAndJoin() + detectorService.scope.launch(detectorService.dispatcher) { + doCheckUnderProgress(project, detectorService) + } + } + + suspend fun doCheckUnderProgress(project: Project, detectorService: FacetDetectorScopeProvider) { - withBackgroundProgress(project, "Detecting Minecraft Frameworks", cancellable = false) { + withBackgroundProgress(project, "Detecting Minecraft Frameworks", cancellable = false) { + detectorService.lock.withLock { + detectorService.currentJob?.cancelAndJoin() detectorService.currentJob = coroutineContext.job + } - doCheck(project) - } - } + doCheck(project) + } + } - } suspend fun doCheck(project: Project) { val moduleManager = ModuleManager.getInstance(project)