From ebef991477f0843fcbb1fb85550ac181e6ab696d Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Tue, 23 Jan 2024 12:33:56 +0100 Subject: [PATCH] [K/N] Workaround hanging during stream draining ^KT-65113 --- .../kotlin/native/executors/HostExecutor.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/native/executors/src/main/kotlin/org/jetbrains/kotlin/native/executors/HostExecutor.kt b/native/executors/src/main/kotlin/org/jetbrains/kotlin/native/executors/HostExecutor.kt index e245c1af610..99367983d87 100644 --- a/native/executors/src/main/kotlin/org/jetbrains/kotlin/native/executors/HostExecutor.kt +++ b/native/executors/src/main/kotlin/org/jetbrains/kotlin/native/executors/HostExecutor.kt @@ -18,6 +18,7 @@ import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.logging.Logger import kotlin.time.* +import kotlin.time.Duration.Companion.seconds fun Logger.debugKt65113(msg: String) { if (!HostManager.hostIsMingw) @@ -189,9 +190,23 @@ class HostExecutor : Executor { streams.drain() ExecuteResponse(null, duration) } else { - logger.info("Finished executing $commandLine in $duration exit code ${process.exitValue()}") - streams.drain() - ExecuteResponse(process.exitValue(), duration) + var exitCode: Int? = process.exitValue() + logger.info("Finished executing $commandLine in $duration exit code $exitCode") + // Workaround for KT-65113 + val waitStreamsDuration = if (HostManager.hostIsMingw) 10.seconds else Duration.INFINITE + try { + withTimeout(waitStreamsDuration) { + streams.drain() + } + } catch (e: TimeoutCancellationException) { + logger.warning("Failed to join the streams in $waitStreamsDuration.") + // TODO(KT-65113): This is here to keep tests failing in the scenario for now. + exitCode = null + streams.cancel() + process.destroyForcibly() + streams.drain() + } + ExecuteResponse(exitCode, duration) } } }