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.

Let the helper's answer through while Peel is busy

Țugui Dragoșb71b8e48b6f6
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
index fce4c3aa..1c687188 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
@@ -140,14 +140,14 @@ public enum PrivilegedHelper {
/// signing checks.
@concurrent
public static func isResponding() async -> Bool {
- await isResponding(over: helperConnection())
+ await isResponding(connecting: helperConnection)
}
static func isResponding(
- over connection: NSXPCConnection?,
+ connecting connect: @escaping @Sendable () -> NSXPCConnection?,
waitingAtMost wait: TimeInterval = answerWait
) async -> Bool {
- let version: Int? = await ask(over: connection, waitingAtMost: wait, fallback: nil) { helper, finish in
+ let version: Int? = await ask(connecting: connect, waitingAtMost: wait, fallback: nil) { helper, finish in
helper.protocolVersion { finish($0) }
}
return version == HelperIdentity.protocolVersion
@@ -294,7 +294,7 @@ public enum PrivilegedHelper {
private static func withHelper<T: Sendable>(
fallback: T,
- _ body: (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
+ _ body: @escaping @Sendable (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
) async -> T {
await request(connecting: helperConnection, fallback: fallback, body)
}
@@ -302,13 +302,13 @@ public enum PrivilegedHelper {
/// Sends a request only once the helper has answered, within its own short wait, on a connection of its own: a
/// helper launchd keeps but cannot start never answers, and the request would wait the longest any request may.
static func request<T: Sendable>(
- connecting connect: () -> NSXPCConnection?,
+ connecting connect: @escaping @Sendable () -> NSXPCConnection?,
answerWait wait: TimeInterval = answerWait,
fallback: T,
- _ body: (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
+ _ body: @escaping @Sendable (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
) async -> T {
- guard await isResponding(over: connect(), waitingAtMost: wait) else { return fallback }
- return await ask(over: connect(), waitingAtMost: requestWait, fallback: fallback, body)
+ guard await isResponding(connecting: connect, waitingAtMost: wait) else { return fallback }
+ return await ask(connecting: connect, waitingAtMost: requestWait, fallback: fallback, body)
}
/// A connection that takes only the helper's own code, or nil when Peel has no team to require.
@@ -322,35 +322,39 @@ public enum PrivilegedHelper {
}
static func ask<T: Sendable>(
- over connection: NSXPCConnection?,
+ connecting connect: @escaping @Sendable () -> NSXPCConnection?,
waitingAtMost wait: TimeInterval,
fallback: T,
- _ body: (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
+ _ body: @escaping @Sendable (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
) async -> T {
- guard let connection else { return fallback }
- connection.remoteObjectInterface = remoteInterface()
- connection.resume()
- defer { connection.invalidate() }
-
- return await withCheckedContinuation { continuation in
- let once = ResumeOnce()
- let finish: @Sendable (T) -> Void = { value in
- if once.claim() {
- continuation.resume(returning: value)
+ // XPC delivers a reply at the quality its message went out with, and never while Peel keeps every thread at
+ // that quality busy, so the message goes out at the timer's quality, whatever the task that asks.
+ await Task.detached(priority: .userInitiated) {
+ guard let connection = connect() else { return fallback }
+ connection.remoteObjectInterface = remoteInterface()
+ connection.resume()
+ defer { connection.invalidate() }
+
+ return await withCheckedContinuation { continuation in
+ let once = ResumeOnce()
+ let finish: @Sendable (T) -> Void = { value in
+ if once.claim() {
+ continuation.resume(returning: value)
+ }
}
+ // A helper that is running but never answers keeps the connection valid, and so does one launchd
+ // keeps and cannot start, so only this timer ends the wait.
+ timers.asyncAfter(deadline: .now() + wait) { finish(fallback) }
+ guard
+ let helper = connection.remoteObjectProxyWithErrorHandler({ _ in finish(fallback) })
+ as? any PeelHelperProtocol
+ else {
+ finish(fallback)
+ return
+ }
+ body(helper, finish)
}
- // A helper that is running but never answers keeps the connection valid, and so does one launchd keeps
- // and cannot start, so only this timer ends the wait.
- timers.asyncAfter(deadline: .now() + wait) { finish(fallback) }
- guard
- let helper = connection.remoteObjectProxyWithErrorHandler({ _ in finish(fallback) })
- as? any PeelHelperProtocol
- else {
- finish(fallback)
- return
- }
- body(helper, finish)
- }
+ }.value
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift
index 4d372c17..b5c7697e 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift
@@ -13,6 +13,7 @@ import Testing
private final class StandInHelper: NSObject, VersionQuestion, NSXPCListenerDelegate {
private let version: Int?
private let listener = NSXPCListener.anonymous()
+ let handledAt = Mutex<UInt32?>(nil)
init(answering version: Int?) {
self.version = version
@@ -21,8 +22,9 @@ private final class StandInHelper: NSObject, VersionQuestion, NSXPCListenerDeleg
listener.resume()
}
- var connection: NSXPCConnection {
- NSXPCConnection(listenerEndpoint: listener.endpoint)
+ var connecting: @Sendable () -> NSXPCConnection? {
+ let endpoint = listener.endpoint
+ return { NSXPCConnection(listenerEndpoint: endpoint) }
}
func stop() {
@@ -30,6 +32,7 @@ private final class StandInHelper: NSObject, VersionQuestion, NSXPCListenerDeleg
}
func protocolVersion(withReply reply: @escaping (Int) -> Void) {
+ handledAt.withLock { $0 = qos_class_self().rawValue }
if let version { reply(version) }
}
@@ -41,6 +44,14 @@ private final class StandInHelper: NSObject, VersionQuestion, NSXPCListenerDeleg
}
}
+private final class Record<Value: Sendable>: Sendable {
+ let value: Mutex<Value>
+
+ init(_ value: Value) {
+ self.value = Mutex(value)
+ }
+}
+
struct HelperAnswerTests {
/// As long as a request waits, so only what the stand-in answers decides, however busy the suite keeps the Mac.
private static let anyWait = PrivilegedHelper.requestWait
@@ -54,7 +65,7 @@ struct HelperAnswerTests {
defer { helper.stop() }
let start = ContinuousClock.now
- #expect(await !PrivilegedHelper.isResponding(over: helper.connection))
+ #expect(await !PrivilegedHelper.isResponding(connecting: helper.connecting))
#expect(ContinuousClock.now - start < .seconds(PrivilegedHelper.requestWait))
}
@@ -62,34 +73,34 @@ struct HelperAnswerTests {
let helper = StandInHelper(answering: HelperIdentity.protocolVersion)
defer { helper.stop() }
- #expect(await PrivilegedHelper.isResponding(over: helper.connection, waitingAtMost: Self.anyWait))
+ #expect(await PrivilegedHelper.isResponding(connecting: helper.connecting, waitingAtMost: Self.anyWait))
}
@Test func aHelperOfAnotherVersionIsNotAnswering() async {
let helper = StandInHelper(answering: HelperIdentity.protocolVersion - 1)
defer { helper.stop() }
- #expect(await !PrivilegedHelper.isResponding(over: helper.connection, waitingAtMost: Self.anyWait))
+ #expect(await !PrivilegedHelper.isResponding(connecting: helper.connecting, waitingAtMost: Self.anyWait))
}
@Test func withNoConnectionThereIsNoAnswer() async {
- #expect(await !PrivilegedHelper.isResponding(over: nil))
+ #expect(await !PrivilegedHelper.isResponding(connecting: { nil }))
}
@Test func aRequestIsNeverSentToAHelperThatDoesNotAnswer() async {
let helper = StandInHelper(answering: nil)
defer { helper.stop() }
- let sent = Mutex(false)
+ let sent = Record(false)
let start = ContinuousClock.now
- let answer = await PrivilegedHelper.request(connecting: { helper.connection }, fallback: "unanswered") {
+ let answer = await PrivilegedHelper.request(connecting: helper.connecting, fallback: "unanswered") {
_, finish in
- sent.withLock { $0 = true }
+ sent.value.withLock { $0 = true }
finish("answered")
}
#expect(answer == "unanswered")
- #expect(!sent.withLock { $0 })
+ #expect(!sent.value.withLock { $0 })
#expect(ContinuousClock.now - start < .seconds(PrivilegedHelper.requestWait))
}
@@ -98,11 +109,29 @@ struct HelperAnswerTests {
defer { helper.stop() }
let answer = await PrivilegedHelper.request(
- connecting: { helper.connection }, answerWait: Self.anyWait, fallback: "unanswered"
+ connecting: helper.connecting, answerWait: Self.anyWait, fallback: "unanswered"
) { _, finish in
finish("answered")
}
#expect(answer == "answered")
}
+
+ @Test func theHelperIsAskedAtTheQualityOfItsWaitWhateverTheTaskThatAsks() async {
+ let helper = StandInHelper(answering: HelperIdentity.protocolVersion)
+ defer { helper.stop() }
+ let connecting = helper.connecting
+ let sentAt = Record<UInt32?>(nil)
+
+ _ = await Task.detached(priority: .background) {
+ await PrivilegedHelper.request(connecting: connecting, answerWait: Self.anyWait, fallback: false) {
+ _, finish in
+ sentAt.value.withLock { $0 = qos_class_self().rawValue }
+ finish(true)
+ }
+ }.value
+
+ #expect((helper.handledAt.withLock { $0 } ?? 0) >= QOS_CLASS_USER_INITIATED.rawValue)
+ #expect((sentAt.value.withLock { $0 } ?? 0) >= QOS_CLASS_USER_INITIATED.rawValue)
+ }
}