1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
import Foundation
/// Safe, asynchronous wrapper around Foundation.Process with deadlock-free pipe reading via readabilityHandler,
/// timeout supervision, and cancellation support.
public final class ProcessRunner: @unchecked Sendable {
private let state = ProcessState()
public init() {}
/// Runs an executable process with arguments, environment, working directory, and timeout.
public func run(
executableURL: URL,
arguments: [String],
workingDirectory: String? = nil,
environment: [String: String] = [:],
timeoutSeconds: TimeInterval? = nil
) async throws -> ExecutionResult {
let startTime = Date()
let proc = Process()
proc.executableURL = executableURL
proc.arguments = arguments
if let workDir = workingDirectory, !workDir.isEmpty {
proc.currentDirectoryURL = URL(fileURLWithPath: (workDir as NSString).expandingTildeInPath)
}
var env = ProcessInfo.processInfo.environment
for (k, v) in environment {
env[k] = v
}
proc.environment = env
let outPipe = Pipe()
let errPipe = Pipe()
proc.standardOutput = outPipe
proc.standardError = errPipe
let stdoutAccumulator = ThreadSafeAccumulator()
let stderrAccumulator = ThreadSafeAccumulator()
outPipe.fileHandleForReading.readabilityHandler = { handle in
let chunk = handle.availableData
if chunk.isEmpty {
outPipe.fileHandleForReading.readabilityHandler = nil
} else {
stdoutAccumulator.append(chunk)
}
}
errPipe.fileHandleForReading.readabilityHandler = { handle in
let chunk = handle.availableData
if chunk.isEmpty {
errPipe.fileHandleForReading.readabilityHandler = nil
} else {
stderrAccumulator.append(chunk)
}
}
try await state.setProcess(proc)
do {
try proc.run()
} catch {
outPipe.fileHandleForReading.readabilityHandler = nil
errPipe.fileHandleForReading.readabilityHandler = nil
_ = await state.clearProcess()
throw ExecutionError.processLaunchFailed(error.localizedDescription)
}
// Timeout supervisor
let timeoutRef = TimeoutRef()
var timeoutTimer: DispatchSourceTimer?
if let timeout = timeoutSeconds, timeout > 0 {
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global(qos: .userInteractive))
timer.schedule(deadline: .now() + timeout)
timer.setEventHandler { [weak proc] in
timeoutRef.markTimedOut()
proc?.terminate()
// Force kill if still unresponsive after 500ms
DispatchQueue.global().asyncAfter(deadline: .now() + 0.5) {
if let pid = proc?.processIdentifier, proc?.isRunning == true {
kill(pid, SIGKILL)
}
}
}
timer.resume()
timeoutTimer = timer
}
// Wait for process exit asynchronously on a global queue
await withCheckedContinuation { continuation in
DispatchQueue.global(qos: .userInitiated).async {
proc.waitUntilExit()
continuation.resume()
}
}
timeoutTimer?.cancel()
timeoutTimer = nil
// Clean up readability handlers
outPipe.fileHandleForReading.readabilityHandler = nil
errPipe.fileHandleForReading.readabilityHandler = nil
// Drain any remaining buffered data
let remainingOut = outPipe.fileHandleForReading.readDataToEndOfFile()
stdoutAccumulator.append(remainingOut)
let remainingErr = errPipe.fileHandleForReading.readDataToEndOfFile()
stderrAccumulator.append(remainingErr)
let duration = Date().timeIntervalSince(startTime)
let stdoutString = String(data: stdoutAccumulator.getData(), encoding: .utf8) ?? ""
let stderrString = String(data: stderrAccumulator.getData(), encoding: .utf8) ?? ""
let wasCancelled = await state.clearProcess()
if wasCancelled {
throw ExecutionError.cancelled
}
if timeoutRef.isTimedOut {
throw ExecutionError.timeout(timeoutSeconds ?? 0)
}
let exitCode = Int(proc.terminationStatus)
if exitCode != 0 {
let combined = stdoutString + (stderrString.isEmpty ? "" : "\n" + stderrString)
throw ExecutionError.nonZeroExitStatus(exitCode, output: combined)
}
return ExecutionResult(
exitCode: exitCode,
stdout: stdoutString,
stderr: stderrString,
duration: duration
)
}
/// Cancels the running process immediately.
public func cancel() {
Task {
let proc = await state.cancel()
if let proc = proc, proc.isRunning {
proc.terminate()
DispatchQueue.global().asyncAfter(deadline: .now() + 0.5) {
if proc.isRunning {
kill(proc.processIdentifier, SIGKILL)
}
}
}
}
}
}
// MARK: - Helper Types
private actor ProcessState {
private var process: Process?
private var isCancelled = false
func setProcess(_ proc: Process) throws {
if isCancelled {
throw ExecutionError.cancelled
}
self.process = proc
}
func cancel() -> Process? {
isCancelled = true
let proc = self.process
self.process = nil
return proc
}
func clearProcess() -> Bool {
let wasCancelled = self.isCancelled
self.process = nil
return wasCancelled
}
}
private final class ThreadSafeAccumulator: @unchecked Sendable {
private var data = Data()
private let lock = NSLock()
func append(_ newChunk: Data) {
guard !newChunk.isEmpty else { return }
lock.lock()
data.append(newChunk)
lock.unlock()
}
func getData() -> Data {
lock.lock()
defer { lock.unlock() }
return data
}
}
private final class TimeoutRef: @unchecked Sendable {
private var timedOut = false
private let lock = NSLock()
var isTimedOut: Bool {
lock.lock()
defer { lock.unlock() }
return timedOut
}
func markTimedOut() {
lock.lock()
timedOut = true
lock.unlock()
}
}
|