Files
openclaw/apps/macos/Sources/OpenClaw/TerminationSignalWatcher.swift
Peter Steinberger 8bcff3bad9 fix(macos): recover after installed app replacement (#108991)
* fix(macos): relaunch trusted app replacements

* refactor(macos): split relocator extension

* fix(macos): revalidate replacement bundle in child

* chore(macos): refresh native i18n inventory

* style(macos): satisfy Swift formatting and dead-code gates
2026-07-16 06:31:02 -07:00

65 lines
2.0 KiB
Swift

import AppKit
import Foundation
import OSLog
enum AppTerminationTiming {
static let cleanupDeadlineSeconds = 2.0
static let signalExitFailsafeSeconds = 3.0
}
@MainActor
final class TerminationSignalWatcher {
static let shared = TerminationSignalWatcher()
private let logger = Logger(subsystem: "ai.openclaw", category: "lifecycle")
private var sources: [DispatchSourceSignal] = []
private var terminationRequested = false
func start() {
guard self.sources.isEmpty else { return }
self.install(SIGTERM)
self.install(SIGINT)
}
func stop() {
for s in self.sources {
s.cancel()
}
self.sources.removeAll(keepingCapacity: false)
self.terminationRequested = false
}
private func install(_ sig: Int32) {
// Make sure the default action doesn't kill the process before we can gracefully shut down.
signal(sig, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: sig, queue: .main)
source.setEventHandler { [weak self] in
self?.handle(sig)
}
source.resume()
self.sources.append(source)
}
private func handle(_ sig: Int32) {
guard !self.terminationRequested else { return }
self.terminationRequested = true
self.logger.info("received signal \(sig, privacy: .public); terminating")
// Ensure any pairing prompt can't accidentally approve during shutdown.
NodePairingApprovalPrompter.shared.stop()
DevicePairingApprovalPrompter.shared.stop()
Self.scheduleExitFailsafe()
NSApp.terminate(nil)
}
static func scheduleExitFailsafe() {
// AppKit waits in a nested event loop while async termination cleanup runs.
// A main-queue failsafe cannot fire from that loop, so enforce the deadline off-main.
DispatchQueue.global(qos: .userInitiated).asyncAfter(
deadline: .now() + AppTerminationTiming.signalExitFailsafeSeconds)
{
exit(0)
}
}
}