Skip to content

Commit 57d2d6e

Browse files
committed
Update container OOMKilled flag immediately
The OOMKilled flag on a container's state has historically behaved rather unintuitively: it is updated on container exit to reflect whether or not any process within the container has been OOM-killed during the preceding run of the container. The OOMKilled flag would be set to true when the container exits if any process within the container---including execs---was OOM-killed at any time while the container was running, whether or not the OOM-kill was the cause of the container exiting. The flag is "sticky," persisting through the next start of the container; only being cleared once the container exits without any processes having been OOM-killed that run. Alter the behavior of the OOMKilled flag such that it signals whether any process in the container had been OOM-killed since the most recent start of the container. Set the flag immediately upon any process being OOM-killed, and clear it when the container transitions to the "running" state. There is an ulterior motive for this change. It reduces the amount of state the libcontainerd client needs to keep track of and clean up on container exit. It's one less place the client could leak memory if a container was to be deleted without going through libcontainerd. Signed-off-by: Cory Snider <[email protected]>
1 parent b752462 commit 57d2d6e

6 files changed

Lines changed: 4 additions & 24 deletions

File tree

api/swagger.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4650,7 +4650,8 @@ definitions:
46504650
example: false
46514651
OOMKilled:
46524652
description: |
4653-
Whether this container has been killed because it ran out of memory.
4653+
Whether a process within this container has been killed because it ran
4654+
out of memory since the container was last started.
46544655
type: "boolean"
46554656
example: false
46564657
Dead:

container/container.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ type ExitStatus struct {
5353
// The exit code with which the container exited.
5454
ExitCode int
5555

56-
// Whether the container encountered an OOM.
57-
OOMKilled bool
58-
5956
// Time at which the container died
6057
ExitedAt time.Time
6158
}

container/state.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func (s *State) SetRunning(pid int, initial bool) {
270270
}
271271
s.ExitCodeValue = 0
272272
s.Pid = pid
273+
s.OOMKilled = false
273274
if initial {
274275
s.StartedAt = time.Now().UTC()
275276
}
@@ -287,7 +288,6 @@ func (s *State) SetStopped(exitStatus *ExitStatus) {
287288
s.FinishedAt = exitStatus.ExitedAt
288289
}
289290
s.ExitCodeValue = exitStatus.ExitCode
290-
s.OOMKilled = exitStatus.OOMKilled
291291

292292
s.notifyAndClear(&s.stopWaiters)
293293
}
@@ -303,7 +303,6 @@ func (s *State) SetRestarting(exitStatus *ExitStatus) {
303303
s.Pid = 0
304304
s.FinishedAt = time.Now().UTC()
305305
s.ExitCodeValue = exitStatus.ExitCode
306-
s.OOMKilled = exitStatus.OOMKilled
307306

308307
s.notifyAndClear(&s.stopWaiters)
309308
}

daemon/monitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (daemon *Daemon) handleContainerExit(c *container.Container, e *libcontaine
4646
if e != nil {
4747
exitStatus.ExitCode = int(e.ExitCode)
4848
exitStatus.ExitedAt = e.ExitedAt
49-
exitStatus.OOMKilled = e.OOMKilled
5049
if e.Error != nil {
5150
c.SetError(e.Error)
5251
}
@@ -141,6 +140,7 @@ func (daemon *Daemon) ProcessEvent(id string, e libcontainerdtypes.EventType, ei
141140

142141
c.Lock()
143142
defer c.Unlock()
143+
c.OOMKilled = true
144144
daemon.updateHealthMonitor(c)
145145
if err := c.CheckpointTo(daemon.containersReplica); err != nil {
146146
return err

libcontainerd/remote/client.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ type client struct {
4747

4848
backend libcontainerdtypes.Backend
4949
eventQ queue.Queue
50-
oomMu sync.Mutex
51-
oom map[string]bool
5250
v2runcoptionsMu sync.Mutex
5351
// v2runcoptions is used for copying options specified on Create() to Start()
5452
v2runcoptions map[string]v2runcoptions.Options
@@ -62,7 +60,6 @@ func NewClient(ctx context.Context, cli *containerd.Client, stateDir, ns string,
6260
logger: logrus.WithField("module", "libcontainerd").WithField("namespace", ns),
6361
ns: ns,
6462
backend: b,
65-
oom: make(map[string]bool),
6663
v2runcoptions: make(map[string]v2runcoptions.Options),
6764
}
6865

@@ -475,9 +472,6 @@ func (c *client) Delete(ctx context.Context, containerID string) error {
475472
if err := ctr.Delete(ctx); err != nil {
476473
return wrapError(err)
477474
}
478-
c.oomMu.Lock()
479-
delete(c.oom, containerID)
480-
c.oomMu.Unlock()
481475
c.v2runcoptionsMu.Lock()
482476
delete(c.v2runcoptions, containerID)
483477
c.v2runcoptionsMu.Unlock()
@@ -767,7 +761,6 @@ func (c *client) processEventStream(ctx context.Context, ns string) {
767761
c.logger.Debug("processing event stream")
768762

769763
for {
770-
var oomKilled bool
771764
select {
772765
case err = <-errC:
773766
if err != nil {
@@ -825,9 +818,7 @@ func (c *client) processEventStream(ctx context.Context, ns string) {
825818
et = libcontainerdtypes.EventOOM
826819
ei = libcontainerdtypes.EventInfo{
827820
ContainerID: t.ContainerID,
828-
OOMKilled: true,
829821
}
830-
oomKilled = true
831822
case *apievents.TaskExecAdded:
832823
et = libcontainerdtypes.EventExecAdded
833824
ei = libcontainerdtypes.EventInfo{
@@ -866,13 +857,6 @@ func (c *client) processEventStream(ctx context.Context, ns string) {
866857
continue
867858
}
868859

869-
c.oomMu.Lock()
870-
if oomKilled {
871-
c.oom[ei.ContainerID] = true
872-
}
873-
ei.OOMKilled = c.oom[ei.ContainerID]
874-
c.oomMu.Unlock()
875-
876860
c.processEvent(ctx, et, ei)
877861
}
878862
}

libcontainerd/types/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type EventInfo struct {
3333
Pid uint32
3434
ExitCode uint32
3535
ExitedAt time.Time
36-
OOMKilled bool
3736
Error error
3837
}
3938

0 commit comments

Comments
 (0)