Skip to content

Commit 4372694

Browse files
Use dispatch group to wait for tasks to complete
1 parent 2cceea8 commit 4372694

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

Sources/SourceKitBazelBSP/SharedUtils/Shell/CommandRunner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension CommandRunner {
3030
_ cmd: String,
3131
cwd: String? = nil,
3232
stdout: Pipe = Pipe(),
33-
stderr: Pipe = Pipe()
33+
stderr: Pipe = Pipe(),
3434
) throws -> RunningProcess {
3535
return try run(cmd, cwd: cwd, stdout: stdout, stderr: stderr)
3636
}
@@ -39,7 +39,7 @@ extension CommandRunner {
3939
_ cmd: String,
4040
cwd: String? = nil,
4141
stdout: Pipe = Pipe(),
42-
stderr: Pipe = Pipe()
42+
stderr: Pipe = Pipe(),
4343
) throws -> T {
4444
let process = try run(cmd, cwd: cwd, stdout: stdout, stderr: stderr)
4545
return try process.output()

Sources/SourceKitBazelBSP/SharedUtils/Shell/RunningProcess.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,47 @@ public struct RunningProcess: Sendable {
5050
}
5151

5252
public func output<T: DataConvertible>() throws -> T {
53-
let dataQueue = DispatchQueue(label: cmd)
5453
var stdoutData: Data = Data()
55-
var stderrData = Data()
54+
var stderrData: Data = Data()
55+
let dataQueue = DispatchQueue(label: "\(cmd)")
56+
let dispatchGroup = DispatchGroup()
5657

58+
dispatchGroup.enter()
5759
stdout.fileHandleForReading.readabilityHandler = { stdoutFileHandle in
5860
let outData = stdoutFileHandle.availableData
59-
if outData.count > 0 {
61+
if outData.isEmpty {
62+
stdoutFileHandle.readabilityHandler = nil
63+
dispatchGroup.leave()
64+
} else {
6065
dataQueue.async {
6166
stdoutData.append(outData)
6267
}
6368
}
6469
}
6570

71+
dispatchGroup.enter()
6672
stderr.fileHandleForReading.readabilityHandler = { stderrFileHandle in
6773
let outData = stderrFileHandle.availableData
68-
if outData.count > 0 {
74+
if outData.isEmpty {
75+
stderrFileHandle.readabilityHandler = nil
76+
dispatchGroup.leave()
77+
} else {
6978
dataQueue.async {
7079
stderrData.append(outData)
7180
}
7281
}
7382
}
7483

7584
wrappedProcess.waitUntilExit()
76-
77-
stdout.fileHandleForReading.readabilityHandler = nil
78-
stderr.fileHandleForReading.readabilityHandler = nil
79-
dataQueue.async {
80-
stdout.fileHandleForReading.closeFile()
81-
stderr.fileHandleForReading.closeFile()
82-
}
83-
85+
dispatchGroup.wait()
86+
8487
guard wrappedProcess.terminationStatus == 0 else {
8588
logger.debug("Command failed: \(cmd)")
86-
let stderrString: String = dataQueue.sync {
87-
String(data: stderrData, encoding: .utf8) ?? "(no stderr)"
88-
}
89+
let stderrString: String = String(data: stderrData, encoding: .utf8) ?? "(no stderr)"
8990
throw ShellCommandRunnerError.failed(cmd, stderrString)
9091
}
9192

92-
return dataQueue.sync { T.convert(from: stdoutData) }
93+
return T.convert(from: stdoutData)
9394
}
9495

9596
public func terminate() {

0 commit comments

Comments
 (0)