From 14460db8e59b023219d9a8292db8244235ce026b Mon Sep 17 00:00:00 2001 From: "Alexander.Likhachev" Date: Thu, 8 Jun 2023 15:41:06 +0200 Subject: [PATCH] [Gradle] Provide relevant error message on daemon crash #KT-59191 Fixed Related to KT-55385 --- .../kotlin/compilerRunner/GradleKotlinCompilerWork.kt | 4 +++- .../org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt index ab7ad8d57fb..b39eafbeb31 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerWork.kt @@ -245,6 +245,8 @@ internal class GradleKotlinCompilerWork @Inject constructor( } bufferingMessageCollector.flush(messageCollector) exitCodeFromProcessExitCode(log, res.get()) + } catch (e: RemoteException) { + throw DaemonCrashedException(e) } catch (e: Throwable) { bufferingMessageCollector.flush(messageCollector) if (e is OutOfMemoryError || e.hasOOMCause()) { @@ -253,7 +255,7 @@ internal class GradleKotlinCompilerWork @Inject constructor( throw e } } finally { - val memoryUsageAfterBuild = daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get() + val memoryUsageAfterBuild = runCatching { daemon.getUsedMemory(withGC = false).takeIf { it.isGood }?.get() }.getOrNull() if (memoryUsageAfterBuild == null || memoryUsageBeforeBuild == null) { log.debug("Unable to calculate memory usage") diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt index 0fc349a940a..dd5885a50c7 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/tasksUtils.kt @@ -43,13 +43,16 @@ internal const val kotlinInProcessOOMHelperMessage = "Not enough memory to run c internal const val kotlinOutOfProcessOOMHelperMessage = "Not enough memory to run compilation." +private const val kotlinDaemonCrashedMessage = + "Connection to the Kotlin daemon has been unexpectedly lost. This might be caused by the daemon being killed by another process or the operating system, or by JVM crash." + internal fun Throwable.hasOOMCause(): Boolean = when (cause) { is OutOfMemoryError -> true else -> cause?.hasOOMCause() ?: false } /** Exception thrown when [ExitCode] != [ExitCode.OK]. */ -internal open class FailedCompilationException(message: String) : RuntimeException(message) +internal open class FailedCompilationException(message: String, cause: Throwable? = null) : RuntimeException(message, cause) /** Exception thrown when [ExitCode] == [ExitCode.COMPILATION_ERROR]. */ internal class CompilationErrorException(message: String) : FailedCompilationException(message) @@ -57,6 +60,9 @@ internal class CompilationErrorException(message: String) : FailedCompilationExc /** Exception thrown when [ExitCode] == [ExitCode.OOM_ERROR]. */ internal class OOMErrorException(message: String) : FailedCompilationException(message) +/** Exception thrown when during the compilation [java.rmi.RemoteException] is caught */ +internal class DaemonCrashedException(cause: Throwable) : FailedCompilationException(kotlinDaemonCrashedMessage, cause) + internal fun TaskWithLocalState.cleanOutputsAndLocalState(reason: String? = null) { val log = GradleKotlinLogger(logger) cleanOutputsAndLocalState(allOutputFiles(), log, metrics.get(), reason)