Skip to content

Commit 08f48d9

Browse files
authored
ContainerSvc: Handle unexpected sandbox svc exits (apple#1065)
Closes apple#1050 If the sandbox svc exits out of band of the usual stop (or regular exit) case the container svc's state is not properly updated for the container. This was due to the cleanup steps involving trying to send the shutdown rpc which cannot succeed as the sandbox svc does not exist to service it. To handle this, let's treat shutdown not returning successfully as non-fatal (as this is mostly best effort), log an error and continue the state cleanup.
1 parent b928e3f commit 08f48d9

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,26 @@ public actor ContainersService {
544544
instanceId: id
545545
)
546546

547-
let client = try state.getClient()
548-
try await client.shutdown()
547+
// Try to shutdown the client gracefully, but if the sandbox service
548+
// is already dead (e.g., killed externally), we should still continue
549+
// with state cleanup.
550+
if let client = state.client {
551+
do {
552+
try await client.shutdown()
553+
} catch {
554+
self.log.error("Failed to shutdown sandbox service for \(id): \(error)")
555+
}
556+
}
549557

550-
// Deregister the service, launchd will terminate the process
551-
try ServiceManager.deregister(fullServiceLabel: label)
552-
self.log.info("Deregistered sandbox service for \(id)")
558+
// Deregister the service, launchd will terminate the process.
559+
// This may also fail if the service was already deregistered or
560+
// the process was killed externally.
561+
do {
562+
try ServiceManager.deregister(fullServiceLabel: label)
563+
self.log.info("Deregistered sandbox service for \(id)")
564+
} catch {
565+
self.log.error("Failed to deregister sandbox service for \(id): \(error)")
566+
}
553567

554568
state.snapshot.status = .stopped
555569
state.snapshot.networks = []

0 commit comments

Comments
 (0)