Skip to content

Commit

Permalink
Merge pull request #17040 from giuseppe/podman-rm-f-no-processes
Browse files Browse the repository at this point in the history
podman: podman rm -f doesn't leave processes
  • Loading branch information
openshift-merge-robot authored Jan 10, 2023
2 parents f451f4f + 4cf06fe commit 5b9e068
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
4 changes: 4 additions & 0 deletions libpod/oci_conmon_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ func (r *ConmonOCIRuntime) StopContainer(ctr *Container, timeout uint, all bool)
}

if err := r.KillContainer(ctr, uint(unix.SIGKILL), all); err != nil {
// If the PID is 0, then the container is already stopped.
if ctr.state.PID == 0 {
return nil
}
// Again, check if the container is gone. If it is, exit cleanly.
if aliveErr := unix.Kill(ctr.state.PID, 0); errors.Is(aliveErr, unix.ESRCH) {
return nil
Expand Down
2 changes: 1 addition & 1 deletion libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func (r *Runtime) removeContainer(ctx context.Context, c *Container, force, remo
}

// Check that the container's in a good state to be removed.
if c.state.State == define.ContainerStateRunning {
if c.ensureState(define.ContainerStateRunning, define.ContainerStateStopping) {
time := c.StopTimeout()
if timeout != nil {
time = *timeout
Expand Down
35 changes: 32 additions & 3 deletions test/system/055-rm.bats
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ load helpers
is "$output" "" "Should print no output"
}

@test "podman container rm doesn't affect stopping containers" {
local cname=c$(random_string 30)
run_podman run -d --name $cname \
function __run_healthcheck_container() {
run_podman run -d --name $1 \
--health-cmd /bin/false \
--health-interval 1s \
--health-retries 2 \
Expand All @@ -125,6 +124,11 @@ load helpers
--health-start-period 0 \
--stop-signal SIGTERM \
$IMAGE sleep infinity
}

@test "podman container rm doesn't affect stopping containers" {
local cname=c$(random_string 30)
__run_healthcheck_container $cname
local cid=$output

# We'll use the PID later to confirm that container is not running
Expand Down Expand Up @@ -159,4 +163,29 @@ load helpers
fi
}

@test "podman container rm --force doesn't leave running processes" {
local cname=c$(random_string 30)
__run_healthcheck_container $cname
local cid=$output

# We'll use the PID later to confirm that container is not running
run_podman inspect --format '{{.State.Pid}}' $cname
local pid=$output

for i in {1..10}; do
run_podman inspect $cname --format '{{.State.Status}}'
if [ "$output" = "stopping" ]; then
break
fi

sleep 0.5
done

run_podman rm -f $cname

if kill -0 $pid; then
die "Container $cname process is still running (pid $pid)"
fi
}

# vim: filetype=sh

0 comments on commit 5b9e068

Please sign in to comment.