[actions] IJPL-221811: Use correct dispatcher for Action's coroutine scope

GitOrigin-RevId: 0ab2955e3780829f62dcea34b5aeddc83988d166
This commit is contained in:
Konstantin Nisht
2025-12-05 10:38:45 +00:00
committed by intellij-monorepo-bot
parent 3ba27d5f52
commit ded6a8aab5
2 changed files with 6 additions and 1 deletions
@@ -1160,7 +1160,7 @@ open class ActionManagerImpl protected constructor(private val coroutineScope: C
ActionContextElement.create(actionId, event.place, event.inputEvent, component)
// todo: remove `ThreadScopeCheckpoint` from here once we migrate all usages to `AnActionEvent#coroutineScope`
val coroutineContext2 = coroutineContext + ThreadScopeCheckpoint(coroutineContext) // permit `currentThreadCoroutineScope` inside
val providedScope = cs.childScope("actionPerformed of $actionId", coroutineContext.minusKey(Job.Key))
val providedScope = cs.childScope("actionPerformed of $actionId", coroutineContext.minusKey(Job.Key) + Dispatchers.Default)
event.installCoroutineScope(providedScope)
try {
installThreadContext(coroutineContext2.minusKey(ContinuationInterceptor), replace = true) {
@@ -10,19 +10,23 @@ import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
@TestApplication
class ActionCoroutineScopeTest {
@OptIn(ExperimentalStdlibApi::class)
@Test
fun `coroutineScope is available in actionPerformed`() = timeoutRunBlocking {
val gotScope = AtomicBoolean(false)
val jobRan = CompletableDeferred<Boolean>()
val scopeDispatcher = AtomicReference<CoroutineDispatcher>()
val action = object : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val scope = e.coroutineScope
gotScope.set(true)
scopeDispatcher.set(scope.coroutineContext[CoroutineDispatcher])
scope.launch {
jobRan.complete(true)
}
@@ -39,6 +43,7 @@ class ActionCoroutineScopeTest {
// Wait for the launched coroutine to run
Assertions.assertTrue(jobRan.awaitWithTimeout(5, TimeUnit.SECONDS) == true,
"Coroutine launched from action scope should run")
Assertions.assertTrue(scopeDispatcher.get() == Dispatchers.Default, "Coroutine scope dispatcher should be Dispatchers.Default")
}
@Test