Skip to content

Commit ea2c31c

Browse files
Merge pull request #14874 from vrothberg/fix-14859
exit code improvements
2 parents 0af75a7 + 3bb4cf8 commit ea2c31c

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

libpod/container_api.go

+4
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ func (c *Container) WaitForExit(ctx context.Context, pollInterval time.Duration)
551551

552552
exitCode, err := c.runtime.state.GetContainerExitCode(id)
553553
if err != nil {
554+
if errors.Is(err, define.ErrNoSuchExitCode) && c.ensureState(define.ContainerStateConfigured, define.ContainerStateCreated) {
555+
// The container never ran.
556+
return true, 0, nil
557+
}
554558
return true, -1, err
555559
}
556560

libpod/container_internal.go

+6
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,12 @@ func (c *Container) cleanupRuntime(ctx context.Context) error {
11041104
return nil
11051105
}
11061106

1107+
// We may be doing this redundantly for some call paths but we need to
1108+
// make sure the exit code is being read at this point.
1109+
if err := c.checkExitFile(); err != nil {
1110+
return err
1111+
}
1112+
11071113
// If necessary, delete attach and ctl files
11081114
if err := c.removeConmonFiles(); err != nil {
11091115
return err

libpod/oci_conmon_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (r *ConmonOCIRuntime) UpdateContainerStatus(ctr *Container) error {
303303
ctr.state.ExitCode = -1
304304
ctr.state.FinishedTime = time.Now()
305305
ctr.state.State = define.ContainerStateExited
306-
return nil
306+
return ctr.runtime.state.AddContainerExitCode(ctr.ID(), ctr.state.ExitCode)
307307
}
308308
return fmt.Errorf("error getting container %s state. stderr/out: %s: %w", ctr.ID(), out, err)
309309
}

test/system/130-kill.bats

+12-1
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,22 @@ load helpers
133133
@test "podman kill - concurrent stop" {
134134
# 14761 - concurrent kill/stop must record the exit code
135135
random_name=$(random_string 10)
136-
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"
136+
run_podman run -d --replace --name=$random_name $IMAGE sh -c "trap 'echo Received SIGTERM, ignoring' SIGTERM; echo READY; while :; do sleep 0.2; done"
137137
$PODMAN stop -t 1 $random_name &
138138
run_podman kill $random_name
139139
run_podman wait $random_name
140140
run_podman rm -f $random_name
141141
}
142142

143+
@test "podman wait - exit codes" {
144+
random_name=$(random_string 10)
145+
run_podman create --name=$random_name $IMAGE /no/such/command
146+
# Container never ran -> exit code == 0
147+
run_podman wait $random_name
148+
# Container did not start successfully -> exit code != 0
149+
run_podman 125 start $random_name
150+
# FIXME(#14873): while older Podmans return 0 on wait, Docker does not.
151+
run_podman wait $random_name
152+
}
153+
143154
# vim: filetype=sh

0 commit comments

Comments
 (0)