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.

Count Terminal as closed as soon as its process has ended

Țugui Dragoș972d82a99570
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2a57cad9..7c0b160b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -73,6 +73,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.
- The Git tab saves its record of what each key held after every key it writes, and quitting Peel waits for the change.
- Turn All Off on the Shell and SSH tabs never writes over a file of Peel's own that couldn't be read.
- The Terminal tab says from the start when Terminal's profiles can't be read, instead of asking you to choose a theme.
+- Quitting Terminal from Peel's question applies the theme at once: Terminal counts as closed as soon as its process has
+ ended, so the question no longer comes back a second time.
#### History
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift b/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift
index d9c0af86..cbffc5fa 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/ProcessEnds.swift
@@ -4,6 +4,14 @@ import os
/// When running programs end, as the kernel tells it. AppKit's `NSRunningApplication.isTerminated` changes only once
/// AppKit hears of the end, which can be long after it.
public enum ProcessEnds {
+ /// Whether the process still runs, as the kernel says: false from the moment it ends, before its parent has
+ /// collected it and before AppKit hears of its end.
+ public static func isRunning(_ identifier: pid_t) -> Bool {
+ var info = proc_bsdinfo()
+ let size = Int32(MemoryLayout<proc_bsdinfo>.size)
+ return proc_pidinfo(identifier, PROC_PIDTBSDINFO, 0, &info, size) == size
+ }
+
/// 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/Terminal/TerminalSettings.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swift
index cc6a47f2..b21cc497 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swift
@@ -7,8 +7,12 @@ public struct TerminalSettings: TerminalSettingsStoring, Sendable {
public init() {}
+ /// Whether Terminal runs. AppKit lists an app for a few milliseconds after its process has ended, and a quit Peel
+ /// waited for has already ended it, so the kernel decides.
public var isTerminalOpen: Bool {
- NSRunningApplication.runningApplications(withBundleIdentifier: Self.identifier).contains { !$0.isTerminated }
+ NSRunningApplication.runningApplications(withBundleIdentifier: Self.identifier).contains {
+ !$0.isTerminated && ProcessEnds.isRunning($0.processIdentifier)
+ }
}
public var isManaged: Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift
index 9c60b122..cf4e424b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ProcessEndsTests.swift
@@ -22,6 +22,37 @@ struct ProcessEndsTests {
}
}
+ @Test func aProgramThatEndedNoLongerRunsEvenBeforeItsParentCollectsIt() async throws {
+ var identifier: pid_t = 0
+ let arguments: [UnsafeMutablePointer<CChar>?] = [strdup("/bin/sleep"), strdup("30"), nil]
+ defer { arguments.forEach { free($0) } }
+ // Started as Foundation starts a program: no signal blocked, every one at its default, so SIGTERM ends it.
+ var attributes: posix_spawnattr_t?
+ posix_spawnattr_init(&attributes)
+ defer { posix_spawnattr_destroy(&attributes) }
+ var none = sigset_t()
+ sigemptyset(&none)
+ var all = sigset_t()
+ sigfillset(&all)
+ posix_spawnattr_setsigmask(&attributes, &none)
+ posix_spawnattr_setsigdefault(&attributes, &all)
+ posix_spawnattr_setflags(&attributes, Int16(POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF))
+ #expect(posix_spawn(&identifier, "/bin/sleep", nil, &attributes, arguments, environ) == 0)
+ defer {
+ kill(identifier, SIGKILL)
+ var status: Int32 = 0
+ waitpid(identifier, &status, 0)
+ }
+ #expect(ProcessEnds.isRunning(identifier))
+
+ let ends = ProcessEnds.of([identifier])
+ kill(identifier, SIGTERM)
+ #expect(await collect(ends) == [identifier])
+
+ #expect(kill(identifier, 0) == 0, "the process is still a zombie, since this test has not collected it")
+ #expect(!ProcessEnds.isRunning(identifier))
+ }
+
@Test func tellsWhenARunningProgramEnds() async throws {
let directory = try TemporaryDirectory()
let program = try directory.runningProgram("Example.app/Contents/MacOS/example")