Skip to content

Commit 68a5316

Browse files
author
aaahuhong
committed
fix(cgroup): prevent process from becoming zombie
Cat process exited early due to unbound stdin and missing reaping by parent, turning it into a zombie. This broke AddProc() to cgroup. The test now also directly uses the current test process PID (os.Getpid()) to verify cgroup attachment. Change-Id: I0579bd82d196391aa7dccbf5676be5e84a386d3e
1 parent 211bf30 commit 68a5316

1 file changed

Lines changed: 5 additions & 30 deletions

File tree

internal/cgroups/cgroups_test.go

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import (
2121
"fmt"
2222
"math"
2323
"os"
24-
"os/exec"
2524
"path/filepath"
2625
"slices"
27-
"syscall"
2826
"testing"
2927
"time"
3028

@@ -40,26 +38,6 @@ func RequireRoot(tb testing.TB) bool {
4038
return os.Geteuid() == 0
4139
}
4240

43-
// cmd.Process.Pid is accessed directly instead of os.Getpid() to avoid
44-
// adding the test process itself into the cgroup, which would skew resource
45-
// accounting for all subsequent assertions.
46-
func StartCatProcess(t *testing.T) *exec.Cmd {
47-
t.Helper()
48-
cmd := exec.Command("cat")
49-
cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL}
50-
if err := cmd.Start(); err != nil {
51-
t.Errorf("start cat process: %v", err)
52-
return nil
53-
}
54-
t.Cleanup(func() {
55-
if cmd.Process != nil {
56-
_ = cmd.Process.Kill()
57-
}
58-
_ = cmd.Wait()
59-
})
60-
return cmd
61-
}
62-
6341
func SetupRuntimeCgroupWithClean(t *testing.T) (Cgroup, string) {
6442
t.Helper()
6543
cgr, err := NewCgroupManager()
@@ -310,12 +288,9 @@ func TestCgroupInterfaces(t *testing.T) {
310288
})
311289

312290
t.Run("AddProcAndPidsAndProcs", func(t *testing.T) {
313-
catCmd := StartCatProcess(t)
314-
if catCmd == nil || catCmd.Process == nil {
315-
t.Fatalf("There is no procs")
316-
}
317-
if err := cgr.AddProc(uint64(catCmd.Process.Pid)); err != nil {
318-
t.Fatalf("AddProc(%d): %v", catCmd.Process.Pid, err)
291+
pid := os.Getpid()
292+
if err := cgr.AddProc(uint64(pid)); err != nil {
293+
t.Fatalf("AddProc(%d): %v", pid, err)
319294
}
320295
processes, err := cgr.Pids(runtimePath)
321296
if err != nil {
@@ -325,8 +300,8 @@ func TestCgroupInterfaces(t *testing.T) {
325300
if err != nil {
326301
t.Fatalf("Procs: %v", err)
327302
}
328-
if !slices.Contains(procs, int32(catCmd.Process.Pid)) {
329-
t.Fatalf("Procs need to contain %v", catCmd.Process.Pid)
303+
if !slices.Contains(procs, int32(pid)) {
304+
t.Fatalf("Procs need to contain %v", pid)
330305
}
331306
if len(processes) == 0 {
332307
t.Errorf("Pids: got empty process list, want at least one pid")

0 commit comments

Comments
 (0)