Skip to content

Commit ab3cd8d

Browse files
committed
runc delete, container.Destroy: kill all processes
(For a container with no private PID namespace, that is). When runc delete (or container.Destroy) is called on a stopped container without private PID namespace and there are processes in its cgroup, kill those. Add a test case. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 7396ca9 commit ab3cd8d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

libcontainer/state_linux.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ type containerState interface {
3535
}
3636

3737
func destroy(c *Container) error {
38+
// Usually, when a container init is gone, all other processes in its
39+
// cgroup are killed by the kernel. This is not the case for a shared
40+
// PID namespace container, which may have some processes left after
41+
// its init is killed or exited.
42+
//
43+
// As the container without init process running is considered stopped,
44+
// and destroy is supposed to remove all the container resources, we need
45+
// to kill those processes here.
46+
if !c.config.Namespaces.IsPrivate(configs.NEWPID) {
47+
_ = signalAllProcesses(c.cgroupManager, unix.SIGKILL)
48+
}
3849
err := c.cgroupManager.Destroy()
3950
if c.intelRdtManager != nil {
4051
if ierr := c.intelRdtManager.Destroy(); err == nil {

tests/integration/delete.bats

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,19 @@ function teardown() {
6262
[ "$status" -eq 0 ]
6363
}
6464

65-
# Issue 4047, case "runc delete -f".
66-
# See also: "kill KILL [host pidns + init gone]" test in kill.bats.
65+
# Issue 4047, case "runc delete".
66+
@test "runc delete [host pidns + init gone]" {
67+
test_runc_delete_host_pidns
68+
}
69+
70+
# Issue 4047, case "runc delete --force" (different code path).
71+
# shellcheck disable=SC2030
6772
@test "runc delete --force [host pidns + init gone]" {
73+
test_runc_delete_host_pidns --force
74+
}
75+
76+
# See also: "kill KILL [host pidns + init gone]" test in kill.bats.
77+
function test_runc_delete_host_pidns() {
6878
requires cgroups_freezer
6979

7080
update_config ' .linux.namespaces -= [{"type": "pid"}]'
@@ -91,6 +101,7 @@ function teardown() {
91101
fi
92102

93103
runc run -d --console-socket "$CONSOLE_SOCKET" test_busybox
104+
# shellcheck disable=SC2031
94105
[ "$status" -eq 0 ]
95106
cgpath=$(get_cgroup_path "pids")
96107
init_pid=$(cat "$cgpath"/cgroup.procs)
@@ -113,10 +124,14 @@ function teardown() {
113124
kill -0 "$p"
114125
done
115126

116-
runc delete -f test_busybox
127+
# Must kill those processes and remove container.
128+
# shellcheck disable=SC2031
129+
runc delete "$@" test_busybox
130+
# shellcheck disable=SC2031
117131
[ "$status" -eq 0 ]
118132

119133
runc state test_busybox
134+
# shellcheck disable=SC2031
120135
[ "$status" -ne 0 ] # "Container does not exist"
121136

122137
# Make sure all processes are gone.

0 commit comments

Comments
 (0)