Skip to content

Commit 44f221e

Browse files
authored
Merge pull request #2633 from chaitanyabandi/2632-fix
Kill processes in cgroup even if process Wait fails
2 parents 10825f7 + 0aa0fae commit 44f221e

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

libcontainer/integration/exec_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"strconv"
1313
"strings"
14+
"syscall"
1415
"testing"
1516

1617
"github.com/opencontainers/runc/libcontainer"
@@ -1660,6 +1661,59 @@ func TestPIDHost(t *testing.T) {
16601661
}
16611662
}
16621663

1664+
func TestPIDHostInitProcessWait(t *testing.T) {
1665+
if testing.Short() {
1666+
return
1667+
}
1668+
1669+
rootfs, err := newRootfs()
1670+
ok(t, err)
1671+
defer remove(rootfs)
1672+
1673+
pidns := "/proc/1/ns/pid"
1674+
1675+
// Run a container with two long-running processes.
1676+
config := newTemplateConfig(rootfs)
1677+
config.Namespaces.Add(configs.NEWPID, pidns)
1678+
container, err := newContainerWithName("test", config)
1679+
ok(t, err)
1680+
defer func() {
1681+
_ = container.Destroy()
1682+
}()
1683+
1684+
process1 := &libcontainer.Process{
1685+
Cwd: "/",
1686+
Args: []string{"sleep", "100"},
1687+
Env: standardEnvironment,
1688+
Init: true,
1689+
}
1690+
err = container.Run(process1)
1691+
ok(t, err)
1692+
1693+
process2 := &libcontainer.Process{
1694+
Cwd: "/",
1695+
Args: []string{"sleep", "100"},
1696+
Env: standardEnvironment,
1697+
Init: false,
1698+
}
1699+
err = container.Run(process2)
1700+
ok(t, err)
1701+
1702+
// Kill the init process and Wait for it.
1703+
err = process1.Signal(syscall.SIGKILL)
1704+
ok(t, err)
1705+
_, err = process1.Wait()
1706+
if err == nil {
1707+
t.Fatal("expected Wait to indicate failure")
1708+
}
1709+
1710+
// The non-init process must've been killed.
1711+
err = process2.Signal(syscall.Signal(0))
1712+
if err == nil || err.Error() != "no such process" {
1713+
t.Fatalf("expected process to have been killed: %v", err)
1714+
}
1715+
}
1716+
16631717
func TestInitJoinPID(t *testing.T) {
16641718
if testing.Short() {
16651719
return

libcontainer/process_linux.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,11 @@ func (p *initProcess) start() (retErr error) {
511511

512512
func (p *initProcess) wait() (*os.ProcessState, error) {
513513
err := p.cmd.Wait()
514-
if err != nil {
515-
return p.cmd.ProcessState, err
516-
}
517514
// we should kill all processes in cgroup when init is died if we use host PID namespace
518515
if p.sharePidns {
519516
signalAllProcesses(p.manager, unix.SIGKILL)
520517
}
521-
return p.cmd.ProcessState, nil
518+
return p.cmd.ProcessState, err
522519
}
523520

524521
func (p *initProcess) terminate() error {

0 commit comments

Comments
 (0)