From 6c5a96a47420059ce7ed1a6a6d6c13085accc90c Mon Sep 17 00:00:00 2001 From: Nick Cooke <36927374+ncooke3@users.noreply.github.com> Date: Thu, 30 Nov 2023 11:58:55 -0500 Subject: [PATCH] [Release Tooling] Fix bug where process is frozen when running commands with chatty outputs (#12160) --- ReleaseTooling/Sources/Utils/ShellUtils.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ReleaseTooling/Sources/Utils/ShellUtils.swift b/ReleaseTooling/Sources/Utils/ShellUtils.swift index 17db91708bd..e3fe860cded 100644 --- a/ReleaseTooling/Sources/Utils/ShellUtils.swift +++ b/ReleaseTooling/Sources/Utils/ShellUtils.swift @@ -147,6 +147,17 @@ public extension Shell { print("----------------- COMMAND OUTPUT -----------------") } task.launch() + // If we are not outputting to the console, there is a possibility that + // the output pipe gets filled (e.g. when running a command that generates + // lots of output). In this scenario, the process will hang and + // `task.waitUntilExit()` will never return. To work around this issue, + // calling `outHandle.readDataToEndOfFile()` before `task.waitUntilExit()` + // will read from the pipe until the process ends. + var outData: Data! + if !outputToConsole { + outData = outHandle.readDataToEndOfFile() + } + task.waitUntilExit() if outputToConsole { print("----------------- END COMMAND OUTPUT -----------------") } @@ -154,7 +165,6 @@ public extension Shell { if outputToConsole { fullOutput = output.joined(separator: "\n") } else { - let outData = outHandle.readDataToEndOfFile() // Force unwrapping since we know it's UTF8 coming from the console. fullOutput = String(data: outData, encoding: .utf8)! }