Skip to content

Commit 3fa8e98

Browse files
committed
Ensure that containers do not get stuck in stopping
The scenario for inducing this is as follows: 1. Start a container with a long stop timeout and a PID1 that ignores SIGTERM 2. Use `podman stop` to stop that container 3. Simultaneously, in another terminal, kill -9 `pidof podman` (the container is now in ContainerStateStopping) 4. Now kill that container's Conmon with SIGKILL. 5. No commands are able to move the container from Stopping to Stopped now. The cause is a logic bug in our exit-file handling logic. Conmon being dead without an exit file causes no change to the state. Add handling for this case that tries to clean up, including stopping the container if it still seems to be running. Fixes containers#19629 Signed-off-by: Matt Heon <mheon@redhat.com>
1 parent ee8ed8d commit 3fa8e98

File tree

2 files changed

+107
-5
lines changed

2 files changed

+107
-5
lines changed

libpod/container_internal.go

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,8 @@ func (c *Container) start(ctx context.Context) error {
13261326
return nil
13271327
}
13281328

1329-
// Internal, non-locking function to stop container
1330-
func (c *Container) stop(timeout uint) error {
1331-
logrus.Debugf("Stopping ctr %s (timeout %d)", c.ID(), timeout)
1332-
1329+
// Whether a container should use `all` when stopping
1330+
func (c *Container) stopWithAll() (bool, error) {
13331331
// If the container is running in a PID Namespace, then killing the
13341332
// primary pid is enough to kill the container. If it is not running in
13351333
// a pid namespace then the OCI Runtime needs to kill ALL processes in
@@ -1345,14 +1343,26 @@ func (c *Container) stop(timeout uint) error {
13451343
// Only do this check if we need to
13461344
unified, err := cgroups.IsCgroup2UnifiedMode()
13471345
if err != nil {
1348-
return err
1346+
return false, err
13491347
}
13501348
if !unified {
13511349
all = false
13521350
}
13531351
}
13541352
}
13551353

1354+
return all, nil
1355+
}
1356+
1357+
// Internal, non-locking function to stop container
1358+
func (c *Container) stop(timeout uint) error {
1359+
logrus.Debugf("Stopping ctr %s (timeout %d)", c.ID(), timeout)
1360+
1361+
all, err := c.stopWithAll()
1362+
if err != nil {
1363+
return err
1364+
}
1365+
13561366
// OK, the following code looks a bit weird but we have to make sure we can stop
13571367
// containers with the restart policy always, to do this we have to set
13581368
// StoppedByUser even when there is nothing to stop right now. This is due to the
@@ -1442,6 +1452,58 @@ func (c *Container) waitForConmonToExitAndSave() error {
14421452
return err
14431453
}
14441454

1455+
// If we are still ContainerStateStopping, conmon exited without
1456+
// creating an exit file. Let's try and handle that here.
1457+
if c.state.State == define.ContainerStateStopping {
1458+
// Is container PID1 still alive?
1459+
if err := unix.Kill(c.state.PID, 0); err == nil {
1460+
// We have a runaway container, unmanaged by
1461+
// Conmon. Invoke OCI runtime stop.
1462+
// Use 0 timeout for immediate SIGKILL as things
1463+
// have gone seriously wrong.
1464+
// Ignore the error from stopWithAll, it's just
1465+
// a cgroup check - more important that we
1466+
// continue.
1467+
// If we wanted to be really fancy here, we
1468+
// could open a pidfd on container PID1 before
1469+
// this to get the real exit code... But I'm not
1470+
// that dedicated.
1471+
all, _ := c.stopWithAll()
1472+
if err := c.ociRuntime.StopContainer(c, 0, all); err != nil {
1473+
logrus.Errorf("Error stopping container %s after Conmon exited prematurely: %v", c.ID(), err)
1474+
}
1475+
}
1476+
1477+
// Conmon is dead. Handle it.
1478+
c.state.State = define.ContainerStateStopped
1479+
c.state.PID = 0
1480+
c.state.ConmonPID = 0
1481+
c.state.FinishedTime = time.Now()
1482+
c.state.ExitCode = -1
1483+
c.state.Exited = true
1484+
1485+
c.state.Error = "conmon died without writing exit file, container exit code could not be retrieved"
1486+
1487+
c.newContainerExitedEvent(c.state.ExitCode)
1488+
1489+
if err := c.save(); err != nil {
1490+
logrus.Errorf("Error saving container %s state after Conmon exited prematurely: %v", c.ID(), err)
1491+
}
1492+
1493+
if err := c.runtime.state.AddContainerExitCode(c.ID(), c.state.ExitCode); err != nil {
1494+
logrus.Errorf("Error saving container %s exit code after Conmon exited prematurely: %v", c.ID(), err)
1495+
}
1496+
1497+
// No Conmon alive to trigger cleanup, and the calls in
1498+
// regular Podman are conditional on no errors.
1499+
// Need to clean up manually.
1500+
if err := c.cleanup(context.Background()); err != nil {
1501+
logrus.Errorf("Error cleaning up container %s after Conmon exited prematurely: %v", c.ID(), err)
1502+
}
1503+
1504+
return fmt.Errorf("container %s conmon exited prematurely, exit code could not be retrieved: %w", c.ID(), define.ErrInternal)
1505+
}
1506+
14451507
return c.save()
14461508
}
14471509

test/system/030-run.bats

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,4 +1465,44 @@ search | $IMAGE |
14651465
is "$output" "Error.*: $expect" "podman emits useful diagnostic when no entrypoint is set"
14661466
}
14671467

1468+
@test "podman run - stopping loop" {
1469+
skip_if_remote "this doesn't work with with the REST service"
1470+
1471+
run_podman run -d --name testctr --stop-timeout 240 $IMAGE sh -c 'echo READY; sleep 999'
1472+
cid="$output"
1473+
wait_for_ready testctr
1474+
1475+
run_podman inspect --format '{{ .State.ConmonPid }}' testctr
1476+
conmon_pid=$output
1477+
1478+
${PODMAN} stop testctr &
1479+
stop_pid=$!
1480+
1481+
timeout=20
1482+
while :;do
1483+
sleep 0.5
1484+
run_podman container inspect --format '{{.State.Status}}' testctr
1485+
if [[ "$output" = "stopping" ]]; then
1486+
break
1487+
fi
1488+
timeout=$((timeout - 1))
1489+
if [[ $timeout == 0 ]]; then
1490+
run_podman ps -a
1491+
die "Timed out waiting for testctr to acknowledge signal"
1492+
fi
1493+
done
1494+
1495+
kill -9 ${stop_pid}
1496+
kill -9 ${conmon_pid}
1497+
1498+
# Unclear why `-t0` is required here, works locally without.
1499+
# But it shouldn't hurt and does make the test pass...
1500+
PODMAN_TIMEOUT=5 run_podman 125 stop -t0 testctr
1501+
is "$output" "Error: container .* conmon exited prematurely, exit code could not be retrieved: internal libpod error" "correct error on missing conmon"
1502+
1503+
# This should be safe because stop is guaranteed to call cleanup?
1504+
run_podman inspect --format "{{ .State.Status }}" testctr
1505+
is "$output" "exited" "container has successfully transitioned to exited state after stop"
1506+
}
1507+
14681508
# vim: filetype=sh

0 commit comments

Comments
 (0)