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.

Stop a stuck tool on time on a busy Mac

Țugui Dragoș677644a9c798
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift b/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift
index 54b9001c..c0ebdebf 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/HelperInterface.swift
@@ -7,7 +7,7 @@ public enum HelperIdentity {
public static let launchdPlistName = "com.tuguidragos.Peel.Helper.plist"
/// Raise it whenever the helper changes, so Peel never works with a helper of another version. The helper
/// refuses requests that carry any other version.
- public static let protocolVersion = 15
+ public static let protocolVersion = 16
}
/// The complete set of privileged operations. The helper validates every argument itself.
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift b/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
index 8517b54b..dcbb7239 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/Subprocess.swift
@@ -37,6 +37,8 @@ public enum Subprocess {
/// How long to wait for the rest of the output once the tool has ended. A process the tool started can
/// outlive it and hold a pipe open, and the answer does not wait for that.
static let drain: TimeInterval = 0.5
+ /// At utility quality these timers would wait behind any utility work queued before them.
+ static let timers = DispatchQueue.global(qos: .userInitiated)
/// Runs `executable` with `arguments`, and stops it after `timeout` seconds, when there is one, or when the task
/// is canceled. When `environment` is given, it replaces Peel's own. Both streams are read while the tool runs,
@@ -78,7 +80,7 @@ public enum Subprocess {
let output = Reader(outputPipe.fileHandleForReading.fileDescriptor, onRead: onOutput)
let errors = errorPipe.map { Reader($0.fileHandleForReading.fileDescriptor, onRead: onOutput) }
if let timeout {
- DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + timeout) { [weak run] in
+ timers.asyncAfter(deadline: .now() + timeout) { [weak run] in
run?.stop(because: .timedOut)
}
}
@@ -145,11 +147,11 @@ public enum Subprocess {
}
guard isFirst else { return }
process.terminate()
- DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + Subprocess.grace) { [self] in
+ Subprocess.timers.asyncAfter(deadline: .now() + Subprocess.grace) { [self] in
guard !state.withLock({ $0.hasEnded }), process.isRunning else { return }
kill(process.processIdentifier, SIGKILL)
// A tool stuck in the kernel survives even `SIGKILL`, so the waiter is let go anyway.
- DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + Subprocess.grace) { [self] in
+ Subprocess.timers.asyncAfter(deadline: .now() + Subprocess.grace) { [self] in
ended()
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
index f81493dc..71df3806 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SubprocessTests.swift
@@ -83,6 +83,10 @@ struct SubprocessTests {
#expect(result == .failure(.timedOut))
}
+ @Test func theTimersThatStopAToolNeverWaitBehindWorkOfLowerQuality() {
+ #expect(Subprocess.timers.label == DispatchQueue.global(qos: .userInitiated).label)
+ }
+
/// What a tool writes reaches whoever asked while the tool still runs, from either stream, so a long run can
/// be followed as it goes.
@Test func handsOverWhatTheToolWritesAsItWrites() async throws {