Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Ask an update's old helper again once it has gone
Țugui Dragoș9fcd7d8804a3
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift b/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swiftindex cbffc5fa..2ca509b4 100644--- a/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift+++ b/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift@@ -12,6 +12,30 @@ public enum ProcessEnds {return proc_pidinfo(identifier, PROC_PIDTBSDINFO, 0, &info, size) == size}+ /// Whether the process ends within `limit` seconds, true at once for one that has ended already. Any process may+ /// watch another one end, root's included (`kevent(2)`, EVFILT_PROC).+ public static func ends(_ identifier: pid_t, within limit: TimeInterval) async -> Bool {+ await withCheckedContinuation { continuation in+ let resumed = OSAllocatedUnfairLock(initialState: false)+ let finish: @Sendable (Bool) -> Void = { ended in+ let isFirst = resumed.withLock { resumed in+ defer { resumed = true }+ return !resumed+ }+ if isFirst { continuation.resume(returning: ended) }+ }+ let watch = Task {+ var ended = false+ for await _ in of([identifier]) { ended = true }+ finish(ended)+ }+ DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + limit) {+ watch.cancel()+ finish(false)+ }+ }+ }+/// Each of `identifiers` as its process ends, at once for one that has ended already, watched from this call on./// The stream finishes once every one has ended.public static func of(_ identifiers: [pid_t]) -> AsyncStream<pid_t> {diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swiftindex f333917a..d460a32c 100644--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift@@ -163,12 +163,27 @@ public enum PrivilegedHelper {static func isResponding(connecting connect: @escaping @Sendable () -> NSXPCConnection?,- waitingAtMost wait: TimeInterval = answerWait+ waitingAtMost wait: TimeInterval = answerWait,+ untilEnded: @escaping @Sendable (pid_t, TimeInterval) async -> Bool = { await ProcessEnds.ends($0, within: $1) }) async -> Bool {- let version: Int? = await ask(connecting: connect, waitingAtMost: wait, fallback: nil) { helper, finish in+ let deadline = ContinuousClock.now.advanced(by: .seconds(wait))+ let left = { ContinuousClock.now.duration(to: deadline) / .seconds(1) }+ let first = await askVersion(connecting: connect, waitingAtMost: wait)+ guard let version = first.value else { return false }+ if version == HelperIdentity.protocolVersion { return true }+ // A helper of another version is one an update left running: it leaves as soon as no connection is open+ // (`HelperLifetime`), and launchd then starts the one this copy carries. Another copy's helper answers again.+ guard let process = first.process, await untilEnded(process, left()) else { return false }+ return await askVersion(connecting: connect, waitingAtMost: left()).value == HelperIdentity.protocolVersion+ }++ private static func askVersion(+ connecting connect: @escaping @Sendable () -> NSXPCConnection?,+ waitingAtMost wait: TimeInterval+ ) async -> (value: Int?, process: pid_t?) {+ await answer(connecting: connect, waitingAtMost: wait, fallback: nil) { helper, finish inhelper.protocolVersion { finish($0) }}- return version == HelperIdentity.protocolVersion}/// How long a request is waited for before it counts as unanswered: longer than any request takes, at most@@ -339,15 +354,25 @@ public enum PrivilegedHelper {fallback: T,_ body: @escaping @Sendable (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void) async -> T {+ await answer(connecting: connect, waitingAtMost: wait, fallback: fallback, body).value+ }++ /// `ask`, with the process that replied, which XPC knows once a message has come from it.+ private static func answer<T: Sendable>(+ connecting connect: @escaping @Sendable () -> NSXPCConnection?,+ waitingAtMost wait: TimeInterval,+ fallback: T,+ _ body: @escaping @Sendable (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void+ ) async -> (value: T, process: pid_t?) {// 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 }+ guard let connection = connect() else { return (fallback, nil) }connection.remoteObjectInterface = remoteInterface()connection.resume()defer { connection.invalidate() }- return await withCheckedContinuation { continuation in+ let value = await withCheckedContinuation { continuation inlet once = ResumeOnce()let finish: @Sendable (T) -> Void = { value inif once.claim() {@@ -366,6 +391,8 @@ public enum PrivilegedHelper {}body(helper, finish)}+ let process = connection.processIdentifier+ return (value, process > 0 ? process : nil)}.value}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swiftindex d1d95b3e..2db4e95b 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperAnswerTests.swift@@ -11,12 +11,18 @@ import Testing}private final class StandInHelper: NSObject, VersionQuestion, NSXPCListenerDelegate {- private let version: Int?+ /// What it answers to each question in turn, the last one again once the others are given.+ private let versions: Mutex<[Int?]>private let listener = NSXPCListener.anonymous()let handledAt = Mutex<UInt32?>(nil)+ let questions = Mutex(0)- init(answering version: Int?) {- self.version = version+ convenience init(answering version: Int?) {+ self.init(answering: [version])+ }++ init(answering versions: [Int?]) {+ self.versions = Mutex(versions)super.init()listener.delegate = selflistener.resume()@@ -33,6 +39,8 @@ private final class StandInHelper: NSObject, VersionQuestion, NSXPCListenerDelegfunc protocolVersion(withReply reply: @escaping (Int) -> Void) {handledAt.withLock { $0 = qos_class_self().rawValue }+ questions.withLock { $0 += 1 }+ let version = versions.withLock { $0.count > 1 ? $0.removeFirst() : $0.first ?? nil }if let version { reply(version) }}@@ -78,11 +86,44 @@ struct HelperAnswerTests {#expect(await PrivilegedHelper.isResponding(connecting: helper.connecting, waitingAtMost: Self.anyWait))}- @Test func aHelperOfAnotherVersionIsNotAnswering() async {+ @Test func aHelperAnUpdateLeftRunningGivesWayToTheOneOfThisVersion() async {+ let helper = StandInHelper(answering: [HelperIdentity.protocolVersion - 1, HelperIdentity.protocolVersion])+ defer { helper.stop() }+ let watched = Record<[pid_t]>([])++ let answering = await PrivilegedHelper.isResponding(+ connecting: helper.connecting, waitingAtMost: Self.anyWait+ ) { process, _ in+ watched.value.withLock { $0.append(process) }+ return true+ }++ #expect(answering)+ #expect(watched.value.withLock { $0 } == [getpid()])+ }++ @Test func aHelperOfAnotherVersionIsAskedOnceMoreOnceItsProcessEnds() async {let helper = StandInHelper(answering: HelperIdentity.protocolVersion - 1)defer { helper.stop() }- #expect(await !PrivilegedHelper.isResponding(connecting: helper.connecting, waitingAtMost: Self.anyWait))+ let answering = await PrivilegedHelper.isResponding(+ connecting: helper.connecting, waitingAtMost: Self.anyWait+ ) { _, _ in true }++ #expect(!answering)+ #expect(helper.questions.withLock { $0 } == 2)+ }++ @Test func aHelperOfAnotherVersionWhoseProcessKeepsRunningIsNotAnswering() async {+ let helper = StandInHelper(answering: [HelperIdentity.protocolVersion - 1, HelperIdentity.protocolVersion])+ defer { helper.stop() }++ let answering = await PrivilegedHelper.isResponding(+ connecting: helper.connecting, waitingAtMost: Self.anyWait+ ) { _, _ in false }++ #expect(!answering)+ #expect(helper.questions.withLock { $0 } == 1)}@Test func withNoConnectionThereIsNoAnswer() async {diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swiftindex cf4e424b..e17599ab 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift@@ -104,6 +104,34 @@ struct ProcessEndsTests {#expect(seen == [first.processIdentifier, second.processIdentifier])}+ @Test func saysAProgramEndedWhenItEndsInTime() async throws {+ let directory = try TemporaryDirectory()+ let program = try directory.runningProgram("Example.app/Contents/MacOS/example")+ let identifier = program.processIdentifier++ let ending = Task { await ProcessEnds.ends(identifier, within: 60) }+ program.terminate()++ #expect(await ending.value)+ }++ @Test func saysAProgramThatHasEndedAlreadyEnded() async throws {+ let directory = try TemporaryDirectory()+ let program = try directory.runningProgram("Example.app/Contents/MacOS/example")+ program.terminate()+ program.waitUntilExit()++ #expect(await ProcessEnds.ends(program.processIdentifier, within: 60))+ }++ @Test func saysAProgramThatKeepsRunningDidNotEndInTime() async throws {+ let directory = try TemporaryDirectory()+ let program = try directory.runningProgram("Example.app/Contents/MacOS/example")+ defer { program.terminate() }++ #expect(await !ProcessEnds.ends(program.processIdentifier, within: 0.1))+ }+@Test func finishesAtOnceWithNoProgramToWatch() async {#expect(await collect(ProcessEnds.of([0, -1]), within: 1) == [])}