CoroutinesInternalError: Fatal exception in coroutines machinery

kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[Dispatchers.IO, com.github.xnchung.squire.service.DaggerComponentService$doWork$dumb$1$1$1@49cb8ea4]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
	at kotlinx.coroutines.DispatchedTask.handleFatalException$kotlinx_coroutines_core(DispatchedTask.kt:130)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:107)
	at kotlinx.coroutines.internal.SoftLimitedDispatcher$Worker.run(SoftLimitedDispatcher.kt:130)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:613)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:1183)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:778)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:765)

the code is

@Service(Service.Level.PROJECT)
internal class DaggerComponentService(private val project: Project, private val coroutineScope: CoroutineScope) {
    companion object {
        fun getInstance(project: Project): DaggerComponentService = project.service()
    }

    private val logger: Logger = thisLogger()

    fun doWork() {
        //        KotlinRestrictedAnalysisService.getInstance(project)
        val dumb = DumbService.getInstance(project).runWhenSmart {
            coroutineScope.launch {
                withContext(Dispatchers.IO) {
                    readActionBlocking {

                        logger.warn("DaggerComponentService is doing work...")
                        // coroutineScope.launch(Dispatchers.EDT) {
                        logger.warn("DaggerComponentService start work...")
                        // withContext(Dispatchers.IO) {
                        logger.warn("DaggerComponentService start work...1")
                        val allScope = GlobalSearchScope.projectScope(project)
                        val annotationFQN = "dagger.Component"

                        val clazz =
                            project.service<JavaPsiFacade>().findClass(annotationFQN, allScope)
                        val searchPsiClasses = clazz?.let { AnnotatedElementsSearch.searchPsiClasses(it, allScope) }
                        logger.warn(searchPsiClasses?.toList().toString())
                        StubIndex.getElements(
                            KotlinAnnotationsIndex.indexKey,
                            "Module",
                            project,
                            allScope,
                            KtAnnotationEntry::class.java
                        ).forEach {
                            logger.warn("KotlinAnnotationsIndex: ${it}")
                        }
                        // }
                    }
                }
            }.invokeOnCompletion {
                logger.warn("doWork() with result: $it")
            }
        }
    }
}

how to fix it?

Off topic: Please do not use readActionBlocking UI Freezes and the Dangers of Non-Cancellable Read Actions in Background Threads - The JetBrains Blog

My guess is that your plugin is bundling its own copy of kotlin coroutines, but with a different version than the platform’s library.
Don’t bundle the library, but rely on the platform’s library instead.

In fact, I’ve tried both readAction and smartReadAction, but the problem persists.

Joachim is probably right, your plugin brings extra kotlinx-coroutines library most likely

18:48:50: Executing 'dependencyInsight --configuration runtimeClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm'…

Calculating task graph as no cached configuration is available for tasks: dependencyInsight --configuration runtimeClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm

> Task :dependencyInsight
No dependencies matching given input were found in configuration ':runtimeClasspath'
18:51:21: Executing 'dependencyInsight --configuration compileClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm'…

Calculating task graph as no cached configuration is available for tasks: dependencyInsight --configuration compileClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm

> Task :dependencyInsight
No dependencies matching given input were found in configuration ':compileClasspath'

BUILD SUCCESSFUL in 8s

no any coroutines lib found in compile and runtime classpath ,but it appears in kotlinBuildToolsApiClasspath and kotlinCompilerClasspath

 Executing 'dependencyInsight --configuration kotlinCompilerClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm'…

Calculating task graph as no cached configuration is available for tasks: dependencyInsight --configuration kotlinCompilerClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm

> Task :dependencyInsight
org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0
  Variant jvmRuntimeElements-published:
    | Attribute Name                     | Provided     | Requested |
    |------------------------------------|--------------|-----------|
    | org.gradle.category                | library      |           |
    | org.gradle.libraryelements         | jar          |           |
    | org.gradle.status                  | release      |           |
    | org.gradle.usage                   | java-runtime |           |
    | org.jetbrains.kotlin.platform.type | jvm          |           |

org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:2.3.0
     \--- kotlinCompilerClasspath
18:57:46: Executing 'dependencyInsight --configuration kotlinBuildToolsApiClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm'…

Calculating task graph as no cached configuration is available for tasks: dependencyInsight --configuration kotlinBuildToolsApiClasspath --dependency org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm

> Task :dependencyInsight
org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0
  Variant jvmRuntimeElements-published:
    | Attribute Name                     | Provided     | Requested |
    |------------------------------------|--------------|-----------|
    | org.gradle.category                | library      |           |
    | org.gradle.libraryelements         | jar          |           |
    | org.gradle.status                  | release      |           |
    | org.gradle.usage                   | java-runtime |           |
    | org.jetbrains.kotlin.platform.type | jvm          |           |

org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.8.0
+--- org.jetbrains.kotlin:kotlin-compiler-embeddable:2.3.0
|    +--- org.jetbrains.kotlin:kotlin-build-tools-impl:2.3.0
|    |    \--- kotlinBuildToolsApiClasspath (requested org.jetbrains.kotlin:kotlin-build-tools-impl:{strictly 2.3.0})
|    \--- org.jetbrains.kotlin:kotlin-compiler-runner:2.3.0
|         \--- org.jetbrains.kotlin:kotlin-build-tools-impl:2.3.0 (*)
\--- org.jetbrains.kotlin:kotlin-compiler-runner:2.3.0 (*)

You can easily check it if you :buildPlugin and inspect the resulting ZIP

I checked the built zip file, and it did not contain any coroutine-related JAR files.

It seems to be a version compatibility issue. I downgraded my Kotlin version from 2.3 to 2.1, and the problem disappeared. Coroutines rely on the compiler and compiler plugins to work, which indirectly depends on the Kotlin version.

Check out our compatibility table:

So when you compile with some platform version, please pick the corresponding Kotlin for build