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
Remove an app whose own agent launchd keeps running
Țugui Dragoșb8ec45740f27
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3ad40366..be543878 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -456,6 +456,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.#### Applications+- An app whose own agent launchd keeps running can be removed, here and with `peel uninstall`: asked to quit, it only+ started again, and the question came back. Moving it stops the job, and Put Back starts it again.- Without Full Disk Access, an app's page no longer makes macOS ask about your photos, since `~/Library/Photos` isn'tlooked into.- On macOS 27, the list keeps its place when the first update arrives or the last one leaves.diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swiftindex ceccdd33..9dfd0736 100644--- a/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift+++ b/Packages/PeelCore/Sources/PeelCommandLine/Commands/AppCommands.swift@@ -467,7 +467,10 @@ struct UninstallCommand: AsyncParsableCommand {}private static func whileRunning(_ target: InstalledApp, among apps: [InstalledApp]) async -> CommandFailure? {- let running = await RunningCopies.belonging(to: target, among: RunningCopies.current, installedApps: apps)+ let belonging = await RunningCopies.belonging(to: target, among: RunningCopies.current, installedApps: apps)+ guard !belonging.isEmpty else { return nil }+ let jobs = await RunningCopies.keptRunningByJobs(inside: [target.url])+ let running = belonging.filter { !jobs.contains($0.identifier) }guard !running.isEmpty else { return nil }let identifiers = Set(running.compactMap { $0.bundleIdentifier ?? $0.bundleURL.map(Output.path) }).sorted().map(Output.plain)diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift b/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swiftindex f9ed56f6..06992df4 100644--- a/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift+++ b/Packages/PeelCore/Sources/PeelCore/Apps/RunningCopies.swift@@ -33,6 +33,17 @@ public enum RunningCopies {}}+ /// The processes launchd keeps running as jobs of `apps`, from inside them. Asked to quit, one only starts again,+ /// so nobody is asked: moving the app stops its job.+ @concurrent+ public static func keptRunningByJobs(inside apps: [URL]) async -> Set<pid_t> {+ var found: Set<pid_t> = []+ for app in apps {+ found.formUnion(await LaunchdCleanup.running(inside: app).map(\.pid))+ }+ return found+ }+/// Returns the processes in `running` that belong to `app` and may be quit. The app itself is known by where it/// runs from: a copy elsewhere is another app, and quitting it would close the one in use. The app's helpers run/// from inside its bundle, or carry an identifier the bundle embeds or one that extends the app's, unless anotherdiff --git a/Peel/Model/BulkRemovalPlan.swift b/Peel/Model/BulkRemovalPlan.swiftindex ccfe2dff..b6cbc512 100644--- a/Peel/Model/BulkRemovalPlan.swift+++ b/Peel/Model/BulkRemovalPlan.swift@@ -86,24 +86,28 @@ final class BulkRemovalPlan {return running.allSatisfy { going.contains($0.url) } ? "Quit these apps before removing them." : "Quit these apps first."}- /// The chosen apps with anything of theirs selected that run now. Files never move out from under a running app,- /// so an app that stays still quits before a file of its own moves.- var runningApps: [InstalledApp] {- let running = RunningCopies.current- return owningSelected.filter { app in+ /// The chosen apps with anything of theirs selected that run now, and their processes, helpers included, each once:+ /// two copies of an app share their helpers. Files never move out from under a running app, so an app that stays+ /// still quits before a file of its own moves. What launchd keeps running as a job of the app only starts again,+ /// and the move stops it, so it isn't asked to quit.+ func toQuit() async -> (apps: [InstalledApp], processes: [NSRunningApplication]) {+ var running = RunningCopies.current+ let owners = owningSelected.filter { app in!RunningCopies.belonging(to: app, among: running, installedApps: installedApps).isEmpty}- }-- /// The processes of `runningApps`, helpers included, each once: two copies of an app share their helpers.- var runningProcesses: [NSRunningApplication] {- let running = RunningCopies.current+ guard !owners.isEmpty else { return ([], []) }+ let jobs = await RunningCopies.keptRunningByJobs(inside: owners.map(\.url))+ running.removeAll { jobs.contains($0.identifier) }var seen: Set<pid_t> = []- return owningSelected.flatMap { app in- RunningCopies.belonging(to: app, among: running, installedApps: installedApps)- .filter { seen.insert($0.identifier).inserted }+ var apps: [InstalledApp] = []+ var processes: [NSRunningApplication] = []+ for app in owners {+ let own = RunningCopies.belonging(to: app, among: running, installedApps: installedApps)+ if !own.isEmpty { apps.append(app) }+ processes += own.filter { seen.insert($0.identifier).inserted }.compactMap { NSRunningApplication(processIdentifier: $0.identifier) }}+ return (apps, processes)}private var owningSelected: [InstalledApp] {diff --git a/Peel/Model/RemovalPlan.swift b/Peel/Model/RemovalPlan.swiftindex 011fd406..0176423b 100644--- a/Peel/Model/RemovalPlan.swift+++ b/Peel/Model/RemovalPlan.swift@@ -42,8 +42,16 @@ final class RemovalPlan {private(set) var total = SizeTotal([])private var leftoverSizes: [URL: (size: Int64, isMeasured: Bool)] = [:]- init(app: InstalledApp) {+ private let jobsKeptRunning: @Sendable ([URL]) async -> Set<pid_t>++ init(+ app: InstalledApp,+ jobsKeptRunning: @escaping @Sendable ([URL]) async -> Set<pid_t> = {+ await RunningCopies.keptRunningByJobs(inside: $0)+ }+ ) {self.app = app+ self.jobsKeptRunning = jobsKeptRunning}var scan: LeftoverScan? {@@ -213,9 +221,13 @@ final class RemovalPlan {return await uninstallation.move(request.urls, using: service)}- /// The app's processes that run now, the helpers it ships included, which quit before any of its files move.- var runningProcesses: [NSRunningApplication] {- RunningCopies.belonging(to: app, among: RunningCopies.current, installedApps: installedApps)+ /// The app's processes that run now, the helpers it ships included, which quit before any of its files move, but+ /// for what launchd keeps running as a job of the app, which only starts again and which the move stops.+ func processesToQuit() async -> [NSRunningApplication] {+ let running = RunningCopies.belonging(to: app, among: RunningCopies.current, installedApps: installedApps)+ guard !running.isEmpty else { return [] }+ let jobs = await jobsKeptRunning([app.url])+ return running.filter { !jobs.contains($0.identifier) }.compactMap { NSRunningApplication(processIdentifier: $0.identifier) }}}diff --git a/Peel/Views/AppDetailView.swift b/Peel/Views/AppDetailView.swiftindex 0f9ed1b5..b0e00d3a 100644--- a/Peel/Views/AppDetailView.swift+++ b/Peel/Views/AppDetailView.swift@@ -492,14 +492,16 @@ struct AppDetailView: View {/// if it had to quit: what it wrote until then was not there when the page scanned.private func requestRemoval() {let plan = plan- let running = plan.runningProcesses- quitting.check(running) {- Task {- if !running.isEmpty {- await rescan()- guard !plan.scanRun.wasStopped else { return }+ Task {+ let running = await plan.processesToQuit()+ quitting.check(running) {+ Task {+ if !running.isEmpty {+ await rescan()+ guard !plan.scanRun.wasStopped else { return }+ }+ plan.question.ask(plan.request)}- plan.question.ask(plan.request)}}}@@ -516,7 +518,7 @@ struct AppDetailView: View {private func remove(_ request: RemovalRequest) async {let plan = plandefer { plan.question.finish() }- guard plan.runningProcesses.isEmpty else { return requestRemoval() }+ guard await plan.processesToQuit().isEmpty else { return requestRemoval() }let result = await progress.run(toMove: request.urls.count) {await QuitGuard.shared.run {// The one step that goes before the move: `tccutil` only finds an app that is still in its place.diff --git a/Peel/Views/MultipleAppsView.swift b/Peel/Views/MultipleAppsView.swiftindex 8e3c9e4a..7cc4c8be 100644--- a/Peel/Views/MultipleAppsView.swift+++ b/Peel/Views/MultipleAppsView.swift@@ -363,20 +363,22 @@ struct MultipleAppsView: View {/// out here, once, rather than with every change of the page: the note compares every chosen app with every other.private func requestRemoval() {let plan = plan- let running = plan.runningProcesses- if !running.isEmpty {- quitTitle = Text(BulkRemovalPlan.quitTitle(running: plan.runningApps, going: plan.selectedURLs))- }- quitting.check(running) {- Task {- if !running.isEmpty {- await rescan()- guard !plan.scanRun.wasStopped else { return }+ Task {+ let (apps, running) = await plan.toQuit()+ if !running.isEmpty {+ quitTitle = Text(BulkRemovalPlan.quitTitle(running: apps, going: plan.selectedURLs))+ }+ quitting.check(running) {+ Task {+ if !running.isEmpty {+ await rescan()+ guard !plan.scanRun.wasStopped else { return }+ }+ let request = plan.request+ questionTitle = Text.movingToTrash(request.urls.count, request.total)+ questionNote = note(about: request.urls)+ plan.question.ask(request)}- let request = plan.request- questionTitle = Text.movingToTrash(request.urls.count, request.total)- questionNote = note(about: request.urls)- plan.question.ask(request)}}}@@ -386,7 +388,7 @@ struct MultipleAppsView: View {private func remove(_ request: RemovalRequest) async {let plan = plandefer { plan.question.finish() }- guard plan.runningProcesses.isEmpty else { return requestRemoval() }+ guard await plan.toQuit().processes.isEmpty else { return requestRemoval() }let result = await progress.run(toMove: request.urls.count) {await QuitGuard.shared.run {// The privacy reset runs before the move, because `tccutil` only finds an app that is still in itsdiff --git a/PeelTests/RemovalPlanTests.swift b/PeelTests/RemovalPlanTests.swiftnew file mode 100644index 00000000..65ebdc27--- /dev/null+++ b/PeelTests/RemovalPlanTests.swift@@ -0,0 +1,19 @@+import AppKit+@testable import Peel+import PeelCore+import Testing++@MainActor+struct RemovalPlanTests {+ private let thisApp = InstalledApp(+ url: Bundle.main.bundleURL, bundleIdentifier: Bundle.main.bundleIdentifier, name: "Peel"+ )++ @Test func asksAnAppToQuitUnlessLaunchdKeepsItRunning() async {+ let open = RemovalPlan(app: thisApp) { _ in [] }+ let kept = RemovalPlan(app: thisApp) { _ in [getpid()] }++ #expect(await open.processesToQuit().contains(.current))+ #expect(await !kept.processesToQuit().contains(.current))+ }+}