[Gradle] Provide relevant error message on daemon crash

#KT-59191 Fixed
Related to KT-55385
This commit is contained in:
Alexander.Likhachev
2023-06-08 15:41:06 +02:00
committed by Space Team
parent 274ac76959
commit 14460db8e5
2 changed files with 10 additions and 2 deletions
@@ -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")
@@ -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)