Skip to content

Commit

Permalink
remote wait: fix "removed" condition
Browse files Browse the repository at this point in the history
The "removed" condition mapped to an undefined state which ultimately
rendered the wait endpoint to return an incorrect exit code.  Instead,
map "removed" to "exited" to make sure Podman returns the expected
exit code.

Fixes: containers#18889
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
  • Loading branch information
vrothberg committed Jun 16, 2023
1 parent 719e322 commit ed24f0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 14 additions & 4 deletions pkg/api/handlers/utils/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,21 @@ var notRunningStates = []define.ContainerStatus{
}

func waitRemoved(ctrWait containerWaitFn) (int32, error) {
code, err := ctrWait(define.ContainerStateUnknown)
if err != nil && errors.Is(err, define.ErrNoSuchCtr) {
return code, nil
var code int32
for {
c, err := ctrWait(define.ContainerStateExited)
if errors.Is(err, define.ErrNoSuchCtr) {
// Make sure to wait until the container has been removed.
break
}
if err != nil {
return code, err
}
// If the container doesn't exist, the return code is -1, so
// only set it in case of success.
code = c
}
return code, err
return code, nil
}

func waitNextExit(ctx context.Context, containerName string) (int32, error) {
Expand Down
11 changes: 8 additions & 3 deletions test/apiv2/26-containersWait.at
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ CTR="WaitTestingCtr"

t POST "containers/nonExistent/wait?condition=next-exit" 404

podman create --name "${CTR}" --entrypoint '["true"]' "${IMAGE}"
# Make sure to test a non-zero exit code (see #18889)
podman create --name "${CTR}" "${IMAGE}" sh -c "exit 3"

t POST "containers/${CTR}/wait?condition=non-existent-cond" 400

Expand All @@ -24,7 +25,7 @@ child_pid=$!

# This will block until the background job completes
t POST "containers/${CTR}/wait?condition=next-exit" 200 \
.StatusCode=0 \
.StatusCode=3 \
.Error=null
wait "${child_pid}"

Expand All @@ -41,6 +42,10 @@ fi
child_pid=$!

t POST "containers/${CTR}/wait?condition=removed" 200 \
.StatusCode=0 \
.StatusCode=3 \
.Error=null
# Make sure the container has really been removed after waiting for
# "condition=removed". This check is racy but should flake in case it doesn't
# work correctly.
t POST "containers/${CTR}/wait?condition=next-exit" 404
wait "${child_pid}"

0 comments on commit ed24f0b

Please sign in to comment.