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 stopped task starts no tool

Țugui Dragoșb0a1a431445e
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift b/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
index 2bad21dd..d147ef76 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
@@ -47,6 +47,9 @@ public enum Subprocess {
environment: [String: String]? = nil,
timeout: TimeInterval
) async -> Result<Output, Failure> {
+ // A task stopped before this step starts nothing: launching the tool only to kill it would hold the Stop
+ // for as long as the kill takes.
+ guard !Task.isCancelled else { return .failure(.canceled) }
let process = Process()
process.executableURL = URL(filePath: executable)
process.arguments = arguments
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
index a0425f3f..204536ac 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
@@ -54,6 +54,18 @@ struct SubprocessTests {
#expect(await task.value == .failure(.canceled))
}
+ /// A task that was already canceled starts no tool at all: a scan stopped between two steps must not launch
+ /// the next one only to kill it, which on a busy Mac takes longer than a Stop is allowed. Asked for a tool
+ /// that is not there, such a task answers that it was canceled, since it never tried to start one.
+ @Test func aCanceledTaskStartsNothing() async {
+ let task = Task { () -> Result<Subprocess.Output, Subprocess.Failure> in
+ withUnsafeCurrentTask { $0?.cancel() }
+ return await Subprocess.run("/no/such/tool", [], timeout: 30)
+ }
+
+ #expect(await task.value == .failure(.canceled))
+ }
+
/// A process the tool starts can outlive it and keep its pipe open. `run` returns without waiting for it.
@Test func doesNotWaitForWhatTheToolLeftRunning() async throws {
let started = ContinuousClock.now