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.

Tell an old helper's end even while Peel is busy

Țugui Dragoșe9734558a7c7
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift b/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift
index 322a1ccf..060cfd3a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift
@@ -24,26 +24,29 @@ public enum ProcessEnds {
}
/// 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).
+ /// watch another one end, root's included (`kevent(2)`, EVFILT_PROC). The end and the deadline both arrive on
+ /// dispatch: a task watching the end could wait for a thread of Swift's pool until the deadline had passed.
public static func ends(_ identifier: pid_t, within limit: TimeInterval) async -> Bool {
- await withCheckedContinuation { continuation in
+ guard identifier > 0 else { return false }
+ return await withCheckedContinuation { continuation in
let resumed = OSAllocatedUnfairLock(initialState: false)
+ let source = DispatchSource.makeProcessSource(
+ identifier: identifier,
+ eventMask: .exit,
+ queue: .global(qos: .userInitiated)
+ )
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)
+ guard isFirst else { return }
+ source.cancel()
+ continuation.resume(returning: ended)
}
+ source.setEventHandler { finish(true) }
+ source.resume()
+ DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + limit) { finish(false) }
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift
index fd8eb322..3c67f025 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift
@@ -124,6 +124,21 @@ struct ProcessEndsTests {
#expect(await ProcessEnds.ends(program.processIdentifier, within: 60))
}
+ @Test func saysAProgramEndedWhileEveryThreadOfItsPriorityIsTaken() async throws {
+ let directory = try TemporaryDirectory()
+ let program = try directory.runningProgram("Example.app/Contents/MacOS/example")
+ program.terminate()
+ program.waitUntilExit()
+ let limit: TimeInterval = 1
+ for _ in 0..<ProcessInfo.processInfo.activeProcessorCount {
+ Task.detached(priority: Task.currentPriority) { Self.takeAThread(for: limit + 0.5) }
+ }
+
+ let ended = await ProcessEnds.ends(program.processIdentifier, within: limit)
+
+ #expect(ended)
+ }
+
@Test func saysAProgramThatKeepsRunningDidNotEndInTime() async throws {
let directory = try TemporaryDirectory()
let program = try directory.runningProgram("Example.app/Contents/MacOS/example")
@@ -162,4 +177,13 @@ struct ProcessEndsTests {
@Test func finishesAtOnceWithNoProgramToWatch() async {
#expect(await collect(ProcessEnds.of([0, -1]), within: 1) == [])
}
+
+ @Test func saysNothingEndedWithNoProgramToWatch() async {
+ #expect(await !ProcessEnds.ends(-1, within: 60))
+ }
+
+ // Swift's pool has one thread per processor for each priority.
+ private static func takeAThread(for seconds: TimeInterval) {
+ Thread.sleep(forTimeInterval: seconds)
+ }
}