From 47854e8b7b2d39d14b65ef23073db4aaa04c4cb9 Mon Sep 17 00:00:00 2001 From: Vera Petrenkova Date: Wed, 4 Jun 2025 18:15:55 +0200 Subject: [PATCH] [find in files] add cancellation support for concurrent tasks and improve resource cleanup - Cancel previous "find usages" and "validation" tasks if new ones are initiated. - Enhance `cancelActivities` to stop all active tasks systematically with a message. GitOrigin-RevId: 74eb2031b2cdd67f4337dbc7f3916db64a409d13 --- platform/find/src/FindAndReplaceExecutorImpl.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/platform/find/src/FindAndReplaceExecutorImpl.kt b/platform/find/src/FindAndReplaceExecutorImpl.kt index 054c0be7e769..a9eb7ce4a675 100644 --- a/platform/find/src/FindAndReplaceExecutorImpl.kt +++ b/platform/find/src/FindAndReplaceExecutorImpl.kt @@ -19,12 +19,14 @@ import com.intellij.usages.UsageInfoAdapter import fleet.rpc.client.RpcTimeoutException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import org.jetbrains.annotations.ApiStatus.Internal @Internal open class FindAndReplaceExecutorImpl(val coroutineScope: CoroutineScope) : FindAndReplaceExecutor { private var validationJob: Job? = null + private var findUsagesJob: Job? = null override fun findUsages( project: Project, @@ -36,7 +38,10 @@ open class FindAndReplaceExecutorImpl(val coroutineScope: CoroutineScope) : Find onFinish: () -> Unit?, ) { if (FindKey.isEnabled) { - coroutineScope.launch { + if (findUsagesJob?.isActive == true) { + findUsagesJob?.cancel("new find request is started") + } + findUsagesJob = coroutineScope.launch { val filesToScanInitially = previousUsages.mapNotNull { (it as? UsageInfoModel)?.model?.fileId?.virtualFile() }.toSet() FindRemoteApi.getInstance().findByModel(findModel, project.projectId(), filesToScanInitially.map { it.rpcId() }).collect { findResult -> @@ -77,7 +82,7 @@ open class FindAndReplaceExecutorImpl(val coroutineScope: CoroutineScope) : Find override fun validateModel(findModel: FindModel, onFinish: (isDirectoryExists: Boolean) -> Any?) { if (validationJob?.isActive == true) { - validationJob?.cancel() + validationJob?.cancel("new validation request is started") } validationJob = coroutineScope.launch { try { @@ -89,6 +94,8 @@ open class FindAndReplaceExecutorImpl(val coroutineScope: CoroutineScope) : Find } override fun cancelActivities() { - validationJob?.cancel() + val message = "cancel all activities for find and replace executor" + validationJob?.cancel(message) + findUsagesJob?.cancel(message) } } \ No newline at end of file