Skip to content

Commit eb48fe7

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

2 files changed

Lines changed: 26 additions & 5 deletions

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
}

Sources/CodexBarCore/UsageFetcher.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,6 @@ private final class CodexRPCClient: @unchecked Sendable {
426426
Self.log.debug("Codex RPC stopping")
427427
self.process.terminate()
428428
}
429-
if let registryToken = self.registryToken {
430-
ProcessRegistry.shared.unregister(registryToken)
431-
self.registryToken = nil
432-
}
433429
}
434430

435431
// MARK: - JSON-RPC helpers

0 commit comments

Comments
 (0)