From cc2213cd34c05da0e1bc864a204ecde2d0498296 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 2 Oct 2024 10:46:47 +0400 Subject: [PATCH] Prevent pipe deadlock when spawning a Process() --- .../Credentials/DockerConfigCredentialsProvider.swift | 3 ++- Sources/tart/MACAddressResolver/ARPCache.swift | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/tart/Credentials/DockerConfigCredentialsProvider.swift b/Sources/tart/Credentials/DockerConfigCredentialsProvider.swift index 7c5ba538..5f74033a 100644 --- a/Sources/tart/Credentials/DockerConfigCredentialsProvider.swift +++ b/Sources/tart/Credentials/DockerConfigCredentialsProvider.swift @@ -39,9 +39,10 @@ class DockerConfigCredentialsProvider: CredentialsProvider { inPipe.fileHandleForWriting.write("\(host)\n".data(using: .utf8)!) inPipe.fileHandleForWriting.closeFile() + let outputData = try outPipe.fileHandleForReading.readToEnd() + process.waitUntilExit() - let outputData = try outPipe.fileHandleForReading.readToEnd() if !(process.terminationReason == .exit && process.terminationStatus == 0) { if let outputData = outputData { print(String(decoding: outputData, as: UTF8.self)) diff --git a/Sources/tart/MACAddressResolver/ARPCache.swift b/Sources/tart/MACAddressResolver/ARPCache.swift index 2ce082a2..b2c983e2 100644 --- a/Sources/tart/MACAddressResolver/ARPCache.swift +++ b/Sources/tart/MACAddressResolver/ARPCache.swift @@ -52,6 +52,11 @@ struct ARPCache { process.standardInput = FileHandle.nullDevice try process.run() + + guard let arpCommandOutput = try pipe.fileHandleForReading.readToEnd() else { + throw ARPCommandYieldedInvalidOutputError(explanation: "empty output") + } + process.waitUntilExit() if !(process.terminationReason == .exit && process.terminationStatus == 0) { @@ -60,10 +65,6 @@ struct ARPCache { terminationStatus: process.terminationStatus) } - guard let arpCommandOutput = try pipe.fileHandleForReading.readToEnd() else { - throw ARPCommandYieldedInvalidOutputError(explanation: "empty output") - } - self.arpCommandOutput = arpCommandOutput }