From 4f64431f10025dc95d72c0d3d3a959832e768e59 Mon Sep 17 00:00:00 2001 From: Ivan Gavrilovic Date: Thu, 20 May 2021 16:35:37 +0100 Subject: [PATCH] KT-46820: Kotlin gradle plugin - prevent multiple threads from registering task ... as that is not supported by the underlying mechanism. Build service that holds info about the incremnetal compilation may be instantiated only during execution, and multiple tasks may try to do that. Because the container which holds info about all build services is not thread-safe, this change adds synchronization. Fixes #KT-46820 --- .../org/jetbrains/kotlin/gradle/tasks/Tasks.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index bb5f27b7f2d..4a68144b9e9 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -120,10 +120,17 @@ class GradleCompileTaskProvider(task: Task) { val projectName: String = task.project.rootProject.name.normalizeForFlagFile() val buildModulesInfo: Provider = run { val modulesInfo = GradleCompilerRunner.buildModulesInfo(task.project.gradle) - task.project.gradle.sharedServices.registerIfAbsent( - IncrementalModuleInfoBuildService.getServiceName(), IncrementalModuleInfoBuildService::class.java - ) { - it.parameters.info.set(modulesInfo) + /** + * See https://youtrack.jetbrains.com/issue/KT-46820. Build service that holds the incremental info may + * be instantiated during execution phase and there could be multiple threads trying to do that. Because the + * underlying mechanism does not support multi-threaded access, we need to add external synchronization. + */ + synchronized(task.project.gradle.sharedServices) { + task.project.gradle.sharedServices.registerIfAbsent( + IncrementalModuleInfoBuildService.getServiceName(), IncrementalModuleInfoBuildService::class.java + ) { + it.parameters.info.set(modulesInfo) + } } } }