Skip to content

Commit 12ebc73

Browse files
committed
Fix lingering cli process: Hard SIGKILL fallback
1 parent 3cc8f81 commit 12ebc73

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

Sources/CodexBarCore/Host/Process/ProcessRegistry.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import Foundation
2+
#if canImport(Darwin)
3+
import Darwin
4+
#else
5+
import Glibc
6+
#endif
27

38
public final class ProcessRegistry: @unchecked Sendable {
49
public static let shared = ProcessRegistry()
@@ -35,12 +40,32 @@ public final class ProcessRegistry: @unchecked Sendable {
3540
self.lock.unlock()
3641

3742
for entry in active {
43+
let pid = entry.process.processIdentifier
3844
if entry.process.isRunning {
3945
entry.process.terminate()
4046
}
41-
if let pgid = entry.processGroup {
47+
48+
if let pgid = entry.processGroup,
49+
pgid > 0,
50+
getpgid(pid) == pgid
51+
{
4252
kill(-pgid, SIGTERM)
4353
}
54+
55+
let waitDeadline = Date().addingTimeInterval(1.0)
56+
while entry.process.isRunning, Date() < waitDeadline {
57+
usleep(100_000)
58+
}
59+
60+
if entry.process.isRunning {
61+
if let pgid = entry.processGroup,
62+
pgid > 0,
63+
getpgid(pid) == pgid
64+
{
65+
kill(-pgid, SIGKILL)
66+
}
67+
kill(pid, SIGKILL)
68+
}
4469
}
4570
}
4671
}

0 commit comments

Comments
 (0)