Skip to content

Commit 79a8f53

Browse files
committed
Keep the exit code when a container stops
The runtime reported it and ExitMonitor acted on it, but nothing stored it, so a container that crashed was indistinguishable from one that was asked to stop. ContainerSnapshot carries exitCode and exitedAt now, populated in handleContainerExit. Both are optional, so a snapshot from before this change - or a container that stopped before the apiserver started - decodes fine and simply has no code.
1 parent abff418 commit 79a8f53

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

Sources/ContainerResource/Container/ContainerSnapshot.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,29 @@ public struct ContainerSnapshot: Codable, Sendable {
3939
public var networks: [Attachment]
4040
/// When the container was started.
4141
public var startedDate: Date?
42+
/// The exit code of the container's initial process, once it has exited.
43+
///
44+
/// The engine already knew this — the runtime reports it and ExitMonitor
45+
/// acts on it — but it was dropped on the floor, so a stopped container was
46+
/// indistinguishable from one that had failed. `nil` while running, and for
47+
/// containers that were already stopped before the apiserver started.
48+
public var exitCode: Int32?
49+
/// When the container's initial process exited.
50+
public var exitedAt: Date?
4251

4352
public init(
4453
configuration: ContainerConfiguration,
4554
status: RuntimeStatus,
4655
networks: [Attachment],
47-
startedDate: Date? = nil
56+
startedDate: Date? = nil,
57+
exitCode: Int32? = nil,
58+
exitedAt: Date? = nil
4859
) {
4960
self.configuration = configuration
5061
self.status = status
5162
self.networks = networks
5263
self.startedDate = startedDate
64+
self.exitCode = exitCode
65+
self.exitedAt = exitedAt
5366
}
5467
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,11 @@ public actor ContainersService {
992992

993993
state.snapshot.status = .stopped
994994
state.snapshot.networks = []
995+
// Keep why it stopped, not just that it did.
996+
if let code {
997+
state.snapshot.exitCode = code.exitCode
998+
state.snapshot.exitedAt = code.exitedAt
999+
}
9951000
state.client = nil
9961001
await self.setContainerState(id, state, context: context)
9971002

0 commit comments

Comments
 (0)