Skip to content

Commit ac52ced

Browse files
committed
Surface lastExitCode on ContainerSnapshot
Adds an optional lastExitCode: Int32? field to ContainerSnapshot, populated in ContainersService.handleContainerExit when the container transitions to .stopped. The exit code is taken from the existing ExitMonitor callback's ExitStatus value. This unblocks Container-Compose's depends_on: condition: service_completed_successfully which currently has to fall back to a stopped-only check because the SDK doesn't surface the underlying process exit code. Wire format note: ContainerSnapshot is marshaled as Codable JSON over XPC. Adding an optional field is forward-compatible; older clients will silently ignore the new key, and newer clients reading from older servers will decode lastExitCode as nil. Scope: in-memory only. The exit code lives in the in-memory ContainerState snapshot for the duration of apiserver uptime. A daemon restart resets all snapshots to .stopped without exit codes (existing behavior). A follow-up patch can add bundle persistence (exit_status.json) if needed to survive restarts. Files: - Sources/ContainerResource/Container/ContainerSnapshot.swift: new field + init parameter (default nil for backward compat at all existing construction sites). - Sources/Services/ContainerAPIService/Server/Containers/ContainersService.swift: one assignment in handleContainerExit's terminal state block.
1 parent 0c06aa7 commit ac52ced

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

Sources/ContainerResource/Container/ContainerSnapshot.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ public struct ContainerSnapshot: Codable, Sendable {
3939
public var networks: [Attachment]
4040
/// When the container was started.
4141
public var startedDate: Date?
42+
/// Last observed exit code from the container's main process.
43+
/// Populated when the container transitions to ``RuntimeStatus/stopped``.
44+
/// `nil` if the container has never exited or its exit was not captured.
45+
public var lastExitCode: Int32?
4246

4347
public init(
4448
configuration: ContainerConfiguration,
4549
status: RuntimeStatus,
4650
networks: [Attachment],
47-
startedDate: Date? = nil
51+
startedDate: Date? = nil,
52+
lastExitCode: Int32? = nil
4853
) {
4954
self.configuration = configuration
5055
self.status = status
5156
self.networks = networks
5257
self.startedDate = startedDate
58+
self.lastExitCode = lastExitCode
5359
}
5460
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ public actor ContainersService {
10111011
}
10121012

10131013
state.snapshot.status = .stopped
1014+
state.snapshot.lastExitCode = code?.exitCode
10141015
state.snapshot.networks = []
10151016
state.client = nil
10161017
state.allocatedAttachments = []

0 commit comments

Comments
 (0)