From 4350044dfa9186a08311bd6c2283cfaf495da50f Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 22:48:50 +0000 Subject: [PATCH] test(mcpx): treat zombies as gone when a script timeout kills its group (#13713) TestScriptViewCommandTimeoutKillsSpawnedWorkers polled syscall.Kill(pid, 0) to confirm the spawned worker died. In a container whose PID 1 does not reap orphans the killed worker stays a zombie, and kill(pid, 0) keeps succeeding on a zombie, so the test failed in Fleet executor containers while the group termination itself worked. Poll processExited instead: /proc//stat state Z (linux) or P_stat == SZOMB (darwin) counts as gone, mirroring internal/e2e's exited helper. scriptview.go is unchanged. The helper has its own oracle test: a running child reads alive, a killed-but-unreaped child (zombie) reads exited, a reaped child reads exited. --- .../daemon/process_exited_darwin_test.go | 20 ++++++++ .../daemon/process_exited_linux_test.go | 25 ++++++++++ .../daemon/process_exited_other_test.go | 13 ++++++ .../internal/daemon/process_exited_test.go | 46 +++++++++++++++++++ pkgs/mcpx/internal/daemon/scriptview_test.go | 6 ++- 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 pkgs/mcpx/internal/daemon/process_exited_darwin_test.go create mode 100644 pkgs/mcpx/internal/daemon/process_exited_linux_test.go create mode 100644 pkgs/mcpx/internal/daemon/process_exited_other_test.go create mode 100644 pkgs/mcpx/internal/daemon/process_exited_test.go diff --git a/pkgs/mcpx/internal/daemon/process_exited_darwin_test.go b/pkgs/mcpx/internal/daemon/process_exited_darwin_test.go new file mode 100644 index 0000000000..224c80a71b --- /dev/null +++ b/pkgs/mcpx/internal/daemon/process_exited_darwin_test.go @@ -0,0 +1,20 @@ +package daemon + +import "golang.org/x/sys/unix" + +// processExited reports whether a process is gone or a zombie; a signal-0 +// probe alone counts a zombie as alive. +// +// Darwin has no /proc. The kernel's own process table is read instead, +// the way mcpclient's procStat does (and the way internal/e2e's exited +// does). Same helper as internal/e2e's exited, in this package because +// the daemon tests are package daemon, not e2e_test. +func processExited(pid int) bool { + kp, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + if err != nil { + return true + } + // SZOMB from ; x/sys/unix does not export it for darwin. + const szomb = 5 + return kp.Proc.P_stat == szomb +} diff --git a/pkgs/mcpx/internal/daemon/process_exited_linux_test.go b/pkgs/mcpx/internal/daemon/process_exited_linux_test.go new file mode 100644 index 0000000000..058c0d4130 --- /dev/null +++ b/pkgs/mcpx/internal/daemon/process_exited_linux_test.go @@ -0,0 +1,25 @@ +package daemon + +import ( + "fmt" + "os" + "strings" +) + +// processExited reports whether a process is gone or a zombie; a signal-0 +// probe alone counts a zombie as alive. +// +// In a container whose PID 1 does not reap orphans, a killed orphan stays +// a zombie and syscall.Kill(pid, 0) keeps succeeding on it, so a test +// that polls signal 0 to confirm a kill never sees the process die +// (#13713). The state field of /proc//stat tells a zombie from a +// live process. Same helper as internal/e2e's exited, in this package +// because the daemon tests are package daemon, not e2e_test. +func processExited(pid int) bool { + b, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid)) + if err != nil { + return true + } + i := strings.LastIndexByte(string(b), ')') + return i+2 < len(b) && b[i+2] == 'Z' +} diff --git a/pkgs/mcpx/internal/daemon/process_exited_other_test.go b/pkgs/mcpx/internal/daemon/process_exited_other_test.go new file mode 100644 index 0000000000..1db505e67b --- /dev/null +++ b/pkgs/mcpx/internal/daemon/process_exited_other_test.go @@ -0,0 +1,13 @@ +//go:build !linux && !darwin + +package daemon + +import "syscall" + +// processExited falls back to signal 0 where there is neither /proc nor +// the darwin sysctl; a zombie reads as alive here, so callers wait for it +// to be reaped. Same helper as internal/e2e's exited, in this package +// because the daemon tests are package daemon, not e2e_test. +func processExited(pid int) bool { + return syscall.Kill(pid, 0) != nil +} diff --git a/pkgs/mcpx/internal/daemon/process_exited_test.go b/pkgs/mcpx/internal/daemon/process_exited_test.go new file mode 100644 index 0000000000..e54f448461 --- /dev/null +++ b/pkgs/mcpx/internal/daemon/process_exited_test.go @@ -0,0 +1,46 @@ +package daemon + +import ( + "os/exec" + "testing" + "time" +) + +// TestProcessExitedTellsALiveProcessFromAGoneOne is the only coverage of +// the processExited helper itself, which +// TestScriptViewCommandTimeoutKillsSpawnedWorkers trusts. +// +// Oracle: the test owns the process's lifecycle. A started, unsignalled +// child is alive; once killed it is a zombie until Wait reaps it; after +// Wait its pid is gone. processExited must read alive, exited, exited. +// A signal-0 probe reads the middle step as alive, which is exactly the +// #13713 failure: in a container whose PID 1 does not reap orphans, the +// killed worker stays a zombie and the timeout test never sees it die. +func TestProcessExitedTellsALiveProcessFromAGoneOne(t *testing.T) { + cmd := exec.Command("sleep", "60") + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + pid := cmd.Process.Pid + t.Cleanup(func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }) + + if processExited(pid) { + t.Fatalf("processExited(%d) reports a running child as exited", pid) + } + if err := cmd.Process.Kill(); err != nil { + t.Fatal(err) + } + deadline := time.Now().Add(5 * time.Second) + for !processExited(pid) { + if time.Now().After(deadline) { + t.Fatalf("processExited(%d) reports a killed, unreaped child as alive", pid) + } + time.Sleep(50 * time.Millisecond) + } + if err := cmd.Wait(); err == nil { + t.Fatal("a killed child should report a non-nil wait status") + } + if !processExited(pid) { + t.Fatalf("processExited(%d) reports a reaped child as alive", pid) + } +} diff --git a/pkgs/mcpx/internal/daemon/scriptview_test.go b/pkgs/mcpx/internal/daemon/scriptview_test.go index 7d34f871b6..607db4e215 100644 --- a/pkgs/mcpx/internal/daemon/scriptview_test.go +++ b/pkgs/mcpx/internal/daemon/scriptview_test.go @@ -1134,7 +1134,11 @@ func TestScriptViewCommandTimeoutKillsSpawnedWorkers(t *testing.T) { t.Cleanup(func() { _ = syscall.Kill(pid, syscall.SIGKILL) }) deadline = time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { - if err := syscall.Kill(pid, 0); err != nil { + // processExited, not a signal-0 probe: in a container whose PID 1 + // does not reap orphans the killed worker stays a zombie and + // Kill(pid, 0) keeps succeeding on it (#13713). A zombie is gone + // as far as the process group is concerned. + if processExited(pid) { return // the worker is gone: the group was terminated } time.Sleep(50 * time.Millisecond)