Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

A tool Peel runs lets go of its pipes as soon as it ends

Țugui Dragoș3956a5e8f394
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift b/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
index e7b3a284..5f2de1bb 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
@@ -63,7 +63,10 @@ public enum Subprocess {
process.standardError = errorPipe
let run = Run(process)
+ // The handler holds the run, which holds the process that holds the handler (`NSTask.h`), so it is let go
+ // once the run is over, and the pipes with it: a pipe's descriptor closes only with the pipe.
process.terminationHandler = { _ in run.ended() }
+ defer { process.terminationHandler = nil }
do {
try process.run()
} catch {
@@ -72,7 +75,9 @@ public enum Subprocess {
let output = Reader(outputPipe.fileHandleForReading.fileDescriptor, onRead: onOutput)
let errors = Reader(errorPipe.fileHandleForReading.fileDescriptor, onRead: onOutput)
if let timeout {
- DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + timeout) { run.stop(because: .timedOut) }
+ DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + timeout) { [weak run] in
+ run?.stop(because: .timedOut)
+ }
}
await withTaskCancellationHandler {
@@ -81,6 +86,9 @@ public enum Subprocess {
run.stop(because: .canceled)
}
let (standardOutput, standardError) = (await output.data(within: drain), await errors.data(within: drain))
+ // Both readers have stopped by now, so nothing reads the descriptors any more.
+ try? outputPipe.fileHandleForReading.close()
+ try? errorPipe.fileHandleForReading.close()
if let failure = run.failure { return .failure(failure) }
return .success(Output(status: process.terminationStatus, standardOutput: standardOutput, standardError: standardError))
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
index b6daf7cf..2490cd68 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
@@ -26,6 +26,19 @@ struct SubprocessTests {
#expect(output.standardError.count == 1_000_000)
}
+ /// Package Receipts runs a tool per package, several at a time, and macOS lets a process keep few files open.
+ @Test func aRunLetsGoOfItsPipesWhenItEndsNotWhenItsTimeIsUp() async {
+ func openDescriptors() -> Int { (try? FileManager.default.contentsOfDirectory(atPath: "/dev/fd"))?.count ?? 0 }
+ let before = openDescriptors()
+
+ for _ in 0..<100 {
+ _ = await Subprocess.run("/usr/bin/true", [], timeout: 600)
+ _ = await Subprocess.run("/no/such/tool", [], timeout: 600)
+ }
+
+ #expect(openDescriptors() - before < 50)
+ }
+
@Test func aToolThatIsNotThereIsAFailureAndNotACrash() async {
let result = await Subprocess.run("/no/such/tool", [], timeout: 5)
guard case .failure(.couldNotStart) = result else {