[K/N] More debug output in HostExecutor for Windows ^KT-65113

This commit is contained in:
Alexander Shabalin
2024-01-24 11:43:14 +01:00
committed by Space Team
parent 5ee726558f
commit ab503d0aff
@@ -37,7 +37,7 @@ class ProcessStreams(
private val ignoreIOErrors = AtomicBoolean(false) private val ignoreIOErrors = AtomicBoolean(false)
private val stdin = jobLauncher { private val stdin = jobLauncher {
stdin.apply { stdin.apply {
copyStreams(this, process.outputStream) copyStreams(null, this, process.outputStream)
close() close()
} }
process.outputStream.close() process.outputStream.close()
@@ -45,7 +45,7 @@ class ProcessStreams(
private val stdout = jobLauncher { private val stdout = jobLauncher {
stdout.apply { stdout.apply {
logger.debugKt65113("Will copy from process.inputStream to stdout") logger.debugKt65113("Will copy from process.inputStream to stdout")
copyStreams(process.inputStream, this) copyStreams(logger, process.inputStream, this)
logger.debugKt65113("Will close stdout") logger.debugKt65113("Will close stdout")
close() close()
} }
@@ -55,19 +55,30 @@ class ProcessStreams(
} }
private val stderr = jobLauncher { private val stderr = jobLauncher {
stderr.apply { stderr.apply {
logger.debugKt65113("Will copy from process.errorStream to stderr") copyStreams(null, process.errorStream, this)
copyStreams(process.errorStream, this)
logger.debugKt65113("Will close stderr")
close() close()
} }
logger.debugKt65113("Will close process.errorStream")
process.errorStream.close() process.errorStream.close()
logger.debugKt65113("Finished stderr job")
} }
private fun copyStreams(from: InputStream, to: OutputStream) { private fun copyStreams(logger: Logger?, from: InputStream, to: OutputStream) {
try { try {
from.copyTo(to) if (logger == null || !HostManager.hostIsMingw) {
from.copyTo(to)
return
}
// TODO(KT-65113): Debug where does the hang happen: infinite loop in copyTo, or inside read()
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
logger.debugKt65113("Will do initial read()")
var bytes = from.read(buffer)
logger.debugKt65113("Read $bytes of ${buffer.size} during initial read()")
while (bytes >= 0) {
logger.debugKt65113("Will write to output stream")
to.write(buffer, 0, bytes)
logger.debugKt65113("Will do next read()")
bytes = from.read(buffer)
logger.debugKt65113("Read $bytes")
}
} catch(e: IOException) { } catch(e: IOException) {
if (ignoreIOErrors.get()) if (ignoreIOErrors.get())
return return
@@ -77,14 +88,12 @@ class ProcessStreams(
suspend fun drain() { suspend fun drain() {
// First finish passing input into the process. // First finish passing input into the process.
logger.debugKt65113("Will join stdin")
stdin.join() stdin.join()
// Now receive all the output in whatever order. // Now receive all the output in whatever order.
logger.debugKt65113("Will join stdout") logger.debugKt65113("Will join stdout")
stdout.join() stdout.join()
logger.debugKt65113("Will join stderr") logger.debugKt65113("Did join stdout")
stderr.join() stderr.join()
logger.debugKt65113("Drained the streams")
} }
fun cancel() { fun cancel() {
@@ -142,17 +151,13 @@ private fun <T> ProcessBuilder.scoped(logger: Logger, block: suspend CoroutineSc
val result = runBlocking(Dispatchers.IO) { val result = runBlocking(Dispatchers.IO) {
block(process) block(process)
} }
logger.debugKt65113("Successfully finished executing the process")
result result
} finally { } finally {
logger.debugKt65113("Will destroy the process")
// Make sure the process is killed even if the current thread was interrupted. // Make sure the process is killed even if the current thread was interrupted.
// e.g. gradle task execution was cancelled by the user pressing ^C // e.g. gradle task execution was cancelled by the user pressing ^C
process.destroyForcibly() process.destroyForcibly()
logger.debugKt65113("Will deregister the process")
// The process is dead, no need to ensure its destruction during the shutdown. // The process is dead, no need to ensure its destruction during the shutdown.
ProcessKiller.deregister(process) ProcessKiller.deregister(process)
logger.debugKt65113("Finished executing altogether")
} }
} }