CLI: Skip stopping containers in system stop if APIServer is down (#650)

Today if you do system stop twice (or if you forgot the server isn't
running) you get bombarded with annoying (and confusing) XPC interrupted
messages. We should skip the container stop and list dance if we can't
talk to the APIServer with a simple ping.
This commit is contained in:
Danny Canter
2025-09-19 08:56:23 -07:00
committed by GitHub
parent 465edb372d
commit 2ef1d8fdbb
@@ -47,36 +47,47 @@ extension Application {
let launchdDomainString = try ServiceManager.getDomainString()
let fullLabel = "\(launchdDomainString)/\(prefix)apiserver"
log.info("stopping containers", metadata: ["stopTimeoutSeconds": "\(Self.stopTimeoutSeconds)"])
var running = true
do {
let containers = try await ClientContainer.list()
let signal = try Signals.parseSignal("SIGTERM")
let opts = ContainerStopOptions(timeoutInSeconds: Self.stopTimeoutSeconds, signal: signal)
let failed = try await ContainerStop.stopContainers(containers: containers, stopOptions: opts)
if !failed.isEmpty {
log.warning("some containers could not be stopped gracefully", metadata: ["ids": "\(failed)"])
}
log.info("checking if APIServer is alive")
_ = try await ClientHealthCheck.ping(timeout: .seconds(5))
} catch {
log.warning("failed to stop all containers", metadata: ["error": "\(error)"])
log.info("APIServer health check failed, skipping bootout")
running = false
}
log.info("waiting for containers to exit")
do {
for _ in 0..<Self.shutdownTimeoutSeconds {
let anyRunning = try await ClientContainer.list()
.contains { $0.status == .running }
guard anyRunning else {
break
if running {
log.info("stopping containers", metadata: ["stopTimeoutSeconds": "\(Self.stopTimeoutSeconds)"])
do {
let containers = try await ClientContainer.list()
let signal = try Signals.parseSignal("SIGTERM")
let opts = ContainerStopOptions(timeoutInSeconds: Self.stopTimeoutSeconds, signal: signal)
let failed = try await ContainerStop.stopContainers(containers: containers, stopOptions: opts)
if !failed.isEmpty {
log.warning("some containers could not be stopped gracefully", metadata: ["ids": "\(failed)"])
}
try await Task.sleep(for: .seconds(1))
} catch {
log.warning("failed to stop all containers", metadata: ["error": "\(error)"])
}
log.info("waiting for containers to exit")
do {
for _ in 0..<Self.shutdownTimeoutSeconds {
let anyRunning = try await ClientContainer.list()
.contains { $0.status == .running }
guard anyRunning else {
break
}
try await Task.sleep(for: .seconds(1))
}
log.info("stopping service", metadata: ["label": "\(fullLabel)"])
try ServiceManager.deregister(fullServiceLabel: fullLabel)
} catch {
log.warning("failed to wait for all containers", metadata: ["error": "\(error)"])
}
} catch {
log.warning("failed to wait for all containers", metadata: ["error": "\(error)"])
}
log.info("stopping service", metadata: ["label": "\(fullLabel)"])
try ServiceManager.deregister(fullServiceLabel: fullLabel)
// Note: The assumption here is that we would have registered the launchd services
// in the same domain as `launchdDomainString`. This is a fairly sane assumption since
// if somehow the launchd domain changed, XPC interactions would not be possible.