Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkgs/mcpx/internal/daemon/process_exited_darwin_test.go
Original file line number Diff line number Diff line change
@@ -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 <sys/proc.h>; x/sys/unix does not export it for darwin.
const szomb = 5
return kp.Proc.P_stat == szomb
}
25 changes: 25 additions & 0 deletions pkgs/mcpx/internal/daemon/process_exited_linux_test.go
Original file line number Diff line number Diff line change
@@ -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/<pid>/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'
}
13 changes: 13 additions & 0 deletions pkgs/mcpx/internal/daemon/process_exited_other_test.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions pkgs/mcpx/internal/daemon/process_exited_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 5 additions & 1 deletion pkgs/mcpx/internal/daemon/scriptview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down