Skip to content

Commit

Permalink
kill: record exit code
Browse files Browse the repository at this point in the history
Make sure to record the exit code after killing a container.
Otherwise, a concurrent `stop` may not record the exit code
and yield the container unusable.

Fixes: containers#14761
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Jul 4, 2022
1 parent a406b95 commit e5e5700
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 1 addition & 2 deletions libpod/container_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@ func (c *Container) Kill(signal uint) error {
}

c.state.StoppedByUser = true

c.newContainerEvent(events.Kill)

return c.save()
return c.recordExitCode()
}

// Attach attaches to a container.
Expand Down
16 changes: 12 additions & 4 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,16 +1314,24 @@ func (c *Container) stop(timeout uint) error {
// state has changed. If so, we should return immediately and log a
// warning.
if c.state.State != define.ContainerStateStopping {
logrus.Warnf(
"Container %q state changed from %q to %q while waiting for it to be stopped: discontinuing stop procedure as another process interfered",
c.ID(), define.ContainerStateStopping, c.state.State,
)
if c.state.State != define.ContainerStateExited {
logrus.Warnf(
"Container %q state changed from %q to %q while waiting for it to be stopped: discontinuing stop procedure as another process interfered",
c.ID(), define.ContainerStateStopping, c.state.State,
)
}
return nil
}

c.newContainerEvent(events.Stop)
c.state.StoppedByUser = true

return c.recordExitCode()
}

// recordExitCode reads the exit code from conmon and records it in the
// database.
func (c *Container) recordExitCode() error {
conmonAlive, err := c.ociRuntime.CheckConmonRunning(c)
if err != nil {
return err
Expand Down
10 changes: 10 additions & 0 deletions test/system/130-kill.bats
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ load helpers
is "$output" $cname
}

@test "podman kill - concurrent stop" {
# 14761 - concurrent kill/stop must record the exit code
random_name=$(random_string 10)
run_podman run -d --replace --name=$random_name alpine sh -c "trap 'echo Received SIGTERM, ignoring' SIGTERM; echo READY; while :; do sleep 0.2; done"
$PODMAN stop -t 1 $random_name &
run_podman kill $random_name
run_podman wait $random_name
run_podman rm -f $random_name
}

# vim: filetype=sh

0 comments on commit e5e5700

Please sign in to comment.