From 45857a342bcec5603adae735868677aa13c0a037 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 23:42:33 -0600 Subject: [PATCH 1/8] fix(command): exclude zombies from darwin process group identity Darwin kill(-pgid, sig) returns EPERM for a zombie-only group, but the matcher counted a zombie leader as a live member. Treat zombies as non-members, match a zombie leader by start time only, and add processIsLive and processGroupHasLivingMembers helpers for all unix platforms. --- pkg/command/process_identity_darwin.go | 69 ++++++++++++++++++++++---- pkg/command/process_identity_linux.go | 40 +++++++++++++++ pkg/command/process_identity_unix.go | 8 +++ 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/pkg/command/process_identity_darwin.go b/pkg/command/process_identity_darwin.go index 17d0657dab..8c04b44d36 100644 --- a/pkg/command/process_identity_darwin.go +++ b/pkg/command/process_identity_darwin.go @@ -94,11 +94,26 @@ func darwinProcessGroupMemberMatches( pgid, wantSession int, identity string, ) (matches, leaderMismatch bool, err error) { - processGroupID := int(process.Eproc.Pgid) - if processGroupID != pgid { + if int(process.Eproc.Pgid) != pgid { return false, false, nil } pid := int(process.Proc.P_pid) + isLeader := pid == pgid + if isLeader && !darwinLeaderStartMatches(process, identity) { + return false, true, nil + } + // kill(-pgid, sig) returns EPERM for a zombie-only group on darwin. + if darwinProcessIsZombie(process) { + return false, false, nil + } + return darwinSessionMatches(pid, isLeader, wantSession) +} + +func darwinSessionMatches( + pid int, + isLeader bool, + wantSession int, +) (matches, leaderMismatch bool, err error) { sessionID, err := darwinSessionID(pid) if errors.Is(err, syscall.ESRCH) { return false, false, nil @@ -106,14 +121,8 @@ func darwinProcessGroupMemberMatches( if err != nil { return false, false, err } - if pid == pgid { - leaderIdentity, err := processTreeIdentity(pid) - if err != nil { - return false, false, err - } - if leaderIdentity != "" && leaderIdentity != identity { - return false, true, nil - } + if isLeader && sessionID != wantSession { + return false, true, nil } return sessionID == wantSession, false, nil } @@ -121,3 +130,43 @@ func darwinProcessGroupMemberMatches( func darwinSessionID(pid int) (int, error) { return unix.Getsid(pid) } + +// x/sys/unix does not export SZOMB. +const darwinProcessStateZombie = 5 + +func darwinProcessIsZombie(process unix.KinfoProc) bool { + return process.Proc.P_stat == darwinProcessStateZombie +} + +func darwinLeaderStartMatches(process unix.KinfoProc, identity string) bool { + parts := strings.Split(identity, ":") + if len(parts) != 3 { + return false + } + return parts[1] == strconv.FormatInt(int64(process.Proc.P_starttime.Sec), 10) && + parts[2] == strconv.FormatInt(int64(process.Proc.P_starttime.Usec), 10) +} + +func processIsLive(pid int) (bool, error) { + process, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + if err != nil { + if processLookupConfirmsExit(pid, err) { + return false, nil + } + return false, err + } + return !darwinProcessIsZombie(*process), nil +} + +func processGroupHasLivingMembers(pgid int) (bool, error) { + processes, err := unix.SysctlKinfoProcSlice("kern.proc.all") + if err != nil { + return false, err + } + for _, process := range processes { + if int(process.Eproc.Pgid) == pgid && !darwinProcessIsZombie(process) { + return true, nil + } + } + return false, nil +} diff --git a/pkg/command/process_identity_linux.go b/pkg/command/process_identity_linux.go index 2ec7e6b6ea..97f15d0777 100644 --- a/pkg/command/process_identity_linux.go +++ b/pkg/command/process_identity_linux.go @@ -263,3 +263,43 @@ func linuxProcessStatFields(stat []byte) ([]string, bool) { } return strings.Fields(statText[closingParen+2:]), true } + +func processIsLive(pid int) (bool, error) { + fields, ok, err := linuxProcessStat(pid) + if err != nil || !ok { + return false, err + } + if len(fields) == 0 { + return false, fmt.Errorf("read process state for process %d", pid) + } + return !linuxProcessStateIsExited(fields[0]), nil +} + +func processGroupHasLivingMembers(pgid int) (bool, error) { + entries, err := os.ReadDir("/proc") + if err != nil { + return false, err + } + for _, entry := range entries { + pid, err := strconv.Atoi(entry.Name()) + if err != nil { + continue + } + fields, ok, err := linuxProcessStat(pid) + if err != nil { + return false, err + } + if !ok || len(fields) == 0 { + continue + } + processGroup, _, ok := linuxProcessGroupIDs(fields) + if ok && processGroup == pgid && !linuxProcessStateIsExited(fields[0]) { + return true, nil + } + } + return false, nil +} + +func linuxProcessStateIsExited(state string) bool { + return state == "Z" || state == "X" +} diff --git a/pkg/command/process_identity_unix.go b/pkg/command/process_identity_unix.go index 5298036421..cfb4801df6 100644 --- a/pkg/command/process_identity_unix.go +++ b/pkg/command/process_identity_unix.go @@ -13,3 +13,11 @@ func processTreeIdentity(_ int) (string, error) { func processGroupMatchesIdentity(_ int, _ string) (bool, error) { return false, errors.New("process tree identity is unsupported on this platform") } + +func processIsLive(_ int) (bool, error) { + return false, errors.New("process liveness is unsupported on this platform") +} + +func processGroupHasLivingMembers(_ int) (bool, error) { + return false, errors.New("process group liveness is unsupported on this platform") +} From f4f177439bcf3b50c3da286f956c0ee8373b9134 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 23:42:33 -0600 Subject: [PATCH 2/8] fix(command): confirm EPERM against saved identity when killing groups Suppress EPERM from a negative-PGID signal only when the saved identity matches no live member. Live or unverifiable groups keep the error. Check leader liveness before reading identity so an exited, unreaped leader is treated as gone, and replace kill(-pgid, 0) group probing with a living-member scan. --- pkg/command/process_supported.go | 64 +++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 17 deletions(-) diff --git a/pkg/command/process_supported.go b/pkg/command/process_supported.go index f5350ed875..b41ccde25b 100644 --- a/pkg/command/process_supported.go +++ b/pkg/command/process_supported.go @@ -114,16 +114,6 @@ func waitForUnidentifiedGroupExit(pid int, identity string) error { } } -// A worker whose leader exited may still leave members behind. -func processGroupExists(pid int) bool { - err := syscall.Kill(-pid, 0) - if isProcessGone(err) { - return false - } - // EPERM means the group exists but belongs to another user. - return true -} - func cleanupExitedProcessRef(ref ProcessRef) error { if ref.TreeKind == ProcessTreeLegacyPID { if ref.Identity == "" { @@ -149,7 +139,11 @@ func terminateUnidentifiedExitedGroup(pid int) error { if pid <= 0 { return fmt.Errorf("invalid worker PID %d", pid) } - if processGroupExists(pid) { + live, err := processGroupHasLivingMembers(pid) + if err != nil { + return fmt.Errorf("check worker group %d: %w", pid, err) + } + if live { return fmt.Errorf( "worker group %d still has processes and no saved identity identifies them", pid, @@ -240,8 +234,18 @@ func killWorkerTreeWithIdentity(parsedPid int, treeName, identity string) error if groupMatches { return signalProcessTree(-parsedPid, false, identity) } + live, err := processIsLive(parsedPid) + if err != nil { + return fmt.Errorf("check worker leader %d for worker %s: %w", parsedPid, treeName, err) + } + if !live { + return nil + } currentIdentity, err := processTreeIdentity(parsedPid) if err != nil { + if isProcessGone(err) { + return nil + } return fmt.Errorf("read process tree identity for worker %d: %w", parsedPid, err) } // The saved group is empty, so a different identity at this PID is a reused process. @@ -300,10 +304,7 @@ func isProcessGone(err error) bool { } func signalProcessTree(target int, graceful bool, identity string) error { - if err := syscall.Kill(target, syscall.SIGTERM); err != nil { - if isProcessGone(err) { - return nil // already exited - } + if err := sendProcessSignal(target, syscall.SIGTERM, identity); err != nil { return fmt.Errorf("send SIGTERM to process target %d: %w", target, err) } if graceful { @@ -316,13 +317,42 @@ func signalProcessTree(target int, graceful bool, identity string) error { if !matches { return nil } - err = syscall.Kill(target, syscall.SIGKILL) - if err != nil && !isProcessGone(err) { + if err := sendProcessSignal(target, syscall.SIGKILL, identity); err != nil { return fmt.Errorf("send SIGKILL to process target %d: %w", target, err) } return nil } +func sendProcessSignal(target int, sig syscall.Signal, identity string) error { + err := syscall.Kill(target, sig) + if err == nil || isProcessGone(err) { + return nil + } + if target < 0 && errors.Is(err, syscall.EPERM) { + return confirmGroupEPERM(-target, identity, err, processGroupMatchesIdentity) + } + return err +} + +func confirmGroupEPERM( + pgid int, + identity string, + killErr error, + matcher func(int, string) (bool, error), +) error { + if identity == "" { + return killErr + } + matches, err := matcher(pgid, identity) + if err != nil { + return fmt.Errorf("verify process group %d after EPERM: %w: %w", pgid, killErr, err) + } + if matches { + return fmt.Errorf("process group %d still has matching processes: %w", pgid, killErr) + } + return nil +} + func signalTargetMatchesIdentity(target int, identity string) (bool, error) { if identity == "" { return false, errors.New("process identity is required before SIGKILL") From 877373f0a3e5cf9b45366416c4e23bbf0bb28a62 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 23:42:33 -0600 Subject: [PATCH 3/8] test(command): synchronize group fixture and cover darwin zombie groups Wait for a readiness file before terminating the SIGTERM-ignoring group, assert on living members instead of kernel group existence, and add regression tests for zombie-only groups, stale identities, and EPERM classification. --- pkg/command/process_identity_darwin_test.go | 73 ++++++++++++ pkg/command/process_test.go | 125 +++++++++++++++++--- 2 files changed, 179 insertions(+), 19 deletions(-) create mode 100644 pkg/command/process_identity_darwin_test.go diff --git a/pkg/command/process_identity_darwin_test.go b/pkg/command/process_identity_darwin_test.go new file mode 100644 index 0000000000..e4adb93578 --- /dev/null +++ b/pkg/command/process_identity_darwin_test.go @@ -0,0 +1,73 @@ +//go:build darwin + +package command + +import ( + "os/exec" + "syscall" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" +) + +func startZombieGroupLeader(t *testing.T) (pid int, identity string) { + t.Helper() + cmd := exec.Command("sh", "-c", "read _") + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + stdin, err := cmd.StdinPipe() + require.NoError(t, err) + require.NoError(t, cmd.Start()) + t.Cleanup(func() { + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + _ = cmd.Wait() + }) + pid = cmd.Process.Pid + identity = processTreeIdentityForTest(t, pid) + + require.NoError(t, stdin.Close()) + require.Eventually(t, func() bool { + proc, err := unix.SysctlKinfoProc("kern.proc.pid", pid) + return err == nil && int(proc.Proc.P_pid) == pid && darwinProcessIsZombie(*proc) + }, 5*time.Second, 10*time.Millisecond, "leader did not become a zombie") + return pid, identity +} + +func TestDarwinZombieLeaderIsNotLiveGroupMember(t *testing.T) { + pid, identity := startZombieGroupLeader(t) + + live, err := processIsLive(pid) + require.NoError(t, err) + assert.False(t, live, "zombie leader reported live") + + living, err := processGroupHasLivingMembers(pid) + require.NoError(t, err) + assert.False(t, living, "zombie-only group reported living members") + + matches, err := processGroupMatchesIdentity(pid, identity) + require.NoError(t, err) + assert.False(t, matches, "zombie-only group matched saved identity") +} + +func TestDarwinKillWorkerTreeOnZombieOnlyGroupSucceeds(t *testing.T) { + pid, identity := startZombieGroupLeader(t) + + require.ErrorIs(t, syscall.Kill(-pid, syscall.SIGTERM), syscall.EPERM) + require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", identity)) +} + +func TestDarwinProcessGroupHasLivingMembersSeesLiveChild(t *testing.T) { + cmd := exec.Command("sleep", "30") + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + require.NoError(t, cmd.Start()) + t.Cleanup(func() { + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + _ = cmd.Wait() + }) + + living, err := processGroupHasLivingMembers(cmd.Process.Pid) + require.NoError(t, err) + assert.True(t, living) +} diff --git a/pkg/command/process_test.go b/pkg/command/process_test.go index 83a24f76d7..5fb9ff8b4f 100644 --- a/pkg/command/process_test.go +++ b/pkg/command/process_test.go @@ -3,6 +3,7 @@ package command import ( + "errors" "os" "os/exec" "path/filepath" @@ -13,6 +14,8 @@ import ( "time" ) +const testSavedIdentity = "1:2:3" + func TestKillTerminatesRunningProcess(t *testing.T) { cmd := exec.Command("sleep", "30") if err := cmd.Start(); err != nil { @@ -285,13 +288,7 @@ func TestTerminateUnidentifiedProcessGroup(t *testing.T) { if err != nil { t.Fatalf("terminateProcessRef: %v", err) } - deadline := time.Now().Add(5 * time.Second) - for processGroupExists(pid) && time.Now().Before(deadline) { - time.Sleep(50 * time.Millisecond) - } - if processGroupExists(pid) { - t.Fatalf("worker group %d still has processes after termination", pid) - } + waitForNoLivingGroupMembers(t, pid) } func TestCleanupExitedIdentitylessGroupReportsSurvivingDescendants(t *testing.T) { @@ -314,24 +311,114 @@ func TestCleanupExitedIdentitylessGroupReportsSurvivingDescendants(t *testing.T) } } -// startSigtermIgnoringGroup starts a group leader that outlives SIGTERM. The -// child is reaped as soon as it exits, the way a supervising process does, so -// an unreaped zombie does not keep the group looking populated. func startSigtermIgnoringGroup(t *testing.T) *exec.Cmd { t.Helper() - cmd := exec.Command("sh", "-c", `trap "" TERM; sleep 30`) + ready := filepath.Join(t.TempDir(), "ready") + // #nosec G204 -- the fixed shell script and all paths are test-controlled. + cmd := exec.Command("sh", "-c", `trap '' TERM; printf 'ready\n' > "$1"; sleep 30`, "sh", ready) cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} if err := cmd.Start(); err != nil { t.Fatalf("start SIGTERM-ignoring group: %v", err) } - reaped := make(chan struct{}) - go func() { - _ = cmd.Wait() - close(reaped) - }() t.Cleanup(func() { - _ = cmd.Process.Kill() - <-reaped + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + _ = cmd.Wait() }) - return cmd + deadline := time.Now().Add(5 * time.Second) + for { + // #nosec G304 -- this test reads only its own temporary readiness file. + data, err := os.ReadFile(ready) + if err == nil && string(data) == "ready\n" { + return cmd + } + if time.Now().After(deadline) { + t.Fatalf("SIGTERM-ignoring group did not report readiness: %v", err) + } + time.Sleep(10 * time.Millisecond) + } +} + +func waitForNoLivingGroupMembers(t *testing.T, pgid int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for { + live, err := processGroupHasLivingMembers(pgid) + if err != nil { + t.Fatalf("check living members of group %d: %v", pgid, err) + } + if !live { + return + } + if time.Now().After(deadline) { + t.Fatalf("worker group %d still has living members after termination", pgid) + } + time.Sleep(50 * time.Millisecond) + } +} + +func TestKillWorkerTreeWithStaleIdentityLeavesLiveLeaderRunning(t *testing.T) { + pid := startUnixTestProcess(t, &syscall.SysProcAttr{Setpgid: true}).Process.Pid + identity := processTreeIdentityForTest(t, pid) + stale := identity[:strings.LastIndex(identity, ":")] + ":0" + if err := killWorkerTreeWithIdentity(pid, "detached-task", stale); err != nil { + t.Fatalf("killWorkerTreeWithIdentity with stale identity: %v", err) + } + if running, err := isRunning(strconv.Itoa(pid)); err != nil || !running { + t.Fatalf("live leader with stale identity: running=%t err=%v, want running", running, err) + } +} + +func TestConfirmGroupEPERMKeepsLiveOrUnverifiedGroupErrors(t *testing.T) { + matchErr := errors.New("enumerate processes") + tests := []struct { + name string + identity string + matcher func(int, string) (bool, error) + wantNil bool + wantErrs []error + }{ + { + name: "live matching member keeps EPERM", + identity: testSavedIdentity, + matcher: func(int, string) (bool, error) { return true, nil }, + wantErrs: []error{syscall.EPERM}, + }, + { + name: "failed verification keeps EPERM and verification error", + identity: testSavedIdentity, + matcher: func(int, string) (bool, error) { return false, matchErr }, + wantErrs: []error{syscall.EPERM, matchErr}, + }, + { + name: "no matching member confirms completion", + identity: testSavedIdentity, + matcher: func(int, string) (bool, error) { return false, nil }, + wantNil: true, + }, + { + name: "missing identity never confirms completion", + identity: "", + matcher: func(int, string) (bool, error) { return false, nil }, + wantErrs: []error{syscall.EPERM}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := confirmGroupEPERM(1234, tc.identity, syscall.EPERM, tc.matcher) + if tc.wantNil { + if err != nil { + t.Fatalf("confirmGroupEPERM = %v, want nil", err) + } + return + } + if err == nil { + t.Fatal("confirmGroupEPERM = nil, want error") + } + for _, want := range tc.wantErrs { + if !errors.Is(err, want) { + t.Fatalf("confirmGroupEPERM = %v, want errors.Is %v", err, want) + } + } + }) + } } From a506b840da7d701e6e4a35a652679585e4469451 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 14 Oct 2026 00:11:30 -0600 Subject: [PATCH 4/8] style(command): split linux living-member scan to satisfy cyclop --- pkg/command/process_identity_linux.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/command/process_identity_linux.go b/pkg/command/process_identity_linux.go index 97f15d0777..de9b412c06 100644 --- a/pkg/command/process_identity_linux.go +++ b/pkg/command/process_identity_linux.go @@ -285,21 +285,23 @@ func processGroupHasLivingMembers(pgid int) (bool, error) { if err != nil { continue } - fields, ok, err := linuxProcessStat(pid) - if err != nil { - return false, err - } - if !ok || len(fields) == 0 { - continue - } - processGroup, _, ok := linuxProcessGroupIDs(fields) - if ok && processGroup == pgid && !linuxProcessStateIsExited(fields[0]) { - return true, nil + member, err := linuxProcessIsLiveGroupMember(pid, pgid) + if err != nil || member { + return member, err } } return false, nil } +func linuxProcessIsLiveGroupMember(pid, pgid int) (bool, error) { + fields, ok, err := linuxProcessStat(pid) + if err != nil || !ok || len(fields) == 0 { + return false, err + } + processGroup, _, ok := linuxProcessGroupIDs(fields) + return ok && processGroup == pgid && !linuxProcessStateIsExited(fields[0]), nil +} + func linuxProcessStateIsExited(state string) bool { return state == "Z" || state == "X" } From dc442b6f825c13bd2856805ca8f0d1d6e7d2bfad Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 14 Oct 2026 11:43:42 -0600 Subject: [PATCH 5/8] fix(command): tolerate unreadable proc entries when scanning group liveness Skip unreadable non-leader /proc entries in processGroupHasLivingMembers and confirm group absence with a single kill(-pgid, 0) probe, reusing the existing unreadable-entry policy. An unreadable leader, failed listing, or a group that may still exist keeps failing closed. Reject nonpositive PGIDs. --- pkg/command/process_identity_linux.go | 33 +++++-- pkg/command/process_identity_linux_test.go | 100 +++++++++++++++++++++ 2 files changed, 126 insertions(+), 7 deletions(-) diff --git a/pkg/command/process_identity_linux.go b/pkg/command/process_identity_linux.go index de9b412c06..148f1cf11f 100644 --- a/pkg/command/process_identity_linux.go +++ b/pkg/command/process_identity_linux.go @@ -275,26 +275,45 @@ func processIsLive(pid int) (bool, error) { return !linuxProcessStateIsExited(fields[0]), nil } +type linuxStatReader func(pid int) ([]string, bool, error) + func processGroupHasLivingMembers(pgid int) (bool, error) { + if pgid <= 0 { + return false, fmt.Errorf("invalid process group %d", pgid) + } entries, err := os.ReadDir("/proc") if err != nil { return false, err } + pids := make([]int, 0, len(entries)) for _, entry := range entries { - pid, err := strconv.Atoi(entry.Name()) + if pid, err := strconv.Atoi(entry.Name()); err == nil { + pids = append(pids, pid) + } + } + return linuxLivingGroupMembers(pids, pgid, linuxProcessStat) +} + +func linuxLivingGroupMembers(pids []int, pgid int, readStat linuxStatReader) (bool, error) { + var skipped error + for _, pid := range pids { + member, err := linuxProcessIsLiveGroupMember(pid, pgid, readStat) if err != nil { + if !linuxCanSkipProcessStatError(pid, pgid, err) { + return false, err + } + skipped = errors.Join(skipped, fmt.Errorf("inspect process %d: %w", pid, err)) continue } - member, err := linuxProcessIsLiveGroupMember(pid, pgid) - if err != nil || member { - return member, err + if member { + return true, nil } } - return false, nil + return linuxProcessGroupMatchResult(pgid, false, skipped) } -func linuxProcessIsLiveGroupMember(pid, pgid int) (bool, error) { - fields, ok, err := linuxProcessStat(pid) +func linuxProcessIsLiveGroupMember(pid, pgid int, readStat linuxStatReader) (bool, error) { + fields, ok, err := readStat(pid) if err != nil || !ok || len(fields) == 0 { return false, err } diff --git a/pkg/command/process_identity_linux_test.go b/pkg/command/process_identity_linux_test.go index 564f59ba46..271b6d7282 100644 --- a/pkg/command/process_identity_linux_test.go +++ b/pkg/command/process_identity_linux_test.go @@ -5,8 +5,12 @@ package command import ( "os" "os/exec" + "strconv" "syscall" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const processStatOpenOperation = "open" @@ -149,3 +153,99 @@ func TestProcessTreeIdentityForMissingProcess(t *testing.T) { t.Fatalf("processTreeIdentity for missing PID = %q, want empty identity", identity) } } + +func fakeLinuxStats(stats map[int][]string, errs map[int]error) linuxStatReader { + return func(pid int) ([]string, bool, error) { + if err, ok := errs[pid]; ok { + return nil, false, err + } + fields, ok := stats[pid] + return fields, ok, nil + } +} + +const absentLinuxGroup = int(^uint32(0) >> 1) + +type livingGroupCase struct { + name string + pgid int + pids []int + stats map[int][]string + errs map[int]error + want bool + wantErr bool +} + +func linuxLivingGroupCases(liveGroup int) []livingGroupCase { + const absent = absentLinuxGroup + denied := &os.PathError{Op: processStatOpenOperation, Err: os.ErrPermission} + member := func(pgid int, state string) []string { + return []string{state, "1", strconv.Itoa(pgid), strconv.Itoa(pgid)} + } + return []livingGroupCase{ + { + name: "readable live member wins over unreadable unrelated entry", + pgid: liveGroup, + pids: []int{10, 11}, + stats: map[int][]string{11: member(liveGroup, "S")}, + errs: map[int]error{10: denied}, + want: true, + }, + { + name: "zombie-only group has no living members", + pgid: absent, + pids: []int{absent}, + stats: map[int][]string{absent: member(absent, "Z")}, + }, + {name: "vanished process is ignored", pgid: absent, pids: []int{10}}, + { + name: "unreadable unrelated entry with absent group is complete", + pgid: absent, + pids: []int{10}, + errs: map[int]error{10: denied}, + }, + { + name: "unreadable unrelated entry with existing group fails closed", + pgid: liveGroup, + pids: []int{10}, + errs: map[int]error{10: denied}, + wantErr: true, + }, + { + name: "unreadable leader fails closed", + pgid: absent, + pids: []int{absent}, + errs: map[int]error{absent: denied}, + wantErr: true, + }, + { + name: "unexpected stat error fails closed", + pgid: absent, + pids: []int{10}, + errs: map[int]error{10: syscall.EIO}, + wantErr: true, + }, + } +} + +func TestLinuxLivingGroupMembers(t *testing.T) { + for _, tc := range linuxLivingGroupCases(startLinuxProcessGroupHelper(t)) { + t.Run(tc.name, func(t *testing.T) { + got, err := linuxLivingGroupMembers(tc.pids, tc.pgid, fakeLinuxStats(tc.stats, tc.errs)) + if tc.wantErr { + require.Error(t, err) + assert.False(t, got) + return + } + require.NoError(t, err) + assert.Equal(t, tc.want, got) + }) + } +} + +func TestProcessGroupHasLivingMembersRejectsNonpositiveGroup(t *testing.T) { + for _, pgid := range []int{0, -1} { + _, err := processGroupHasLivingMembers(pgid) + require.Error(t, err) + } +} From b02220503b687f734d5eba2ec7f397479734bc59 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 14 Oct 2026 11:43:42 -0600 Subject: [PATCH 6/8] fix(command): skip grace wait when SIGTERM target is already gone sendProcessSignal now reports signalSent or targetGone so signalProcessTree returns immediately on ESRCH or a confirmed zombie-only group instead of sleeping through the grace period. EPERM handling is unchanged and still limited to identity-confirmed negative targets. --- pkg/command/process_supported.go | 54 ++++++++++++++---- pkg/command/process_test.go | 94 ++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 10 deletions(-) diff --git a/pkg/command/process_supported.go b/pkg/command/process_supported.go index b41ccde25b..8ddeced62b 100644 --- a/pkg/command/process_supported.go +++ b/pkg/command/process_supported.go @@ -303,10 +303,21 @@ func isProcessGone(err error) bool { return err != nil && errors.Is(err, syscall.ESRCH) } +type signalOutcome int + +const ( + signalSent signalOutcome = iota + targetGone +) + func signalProcessTree(target int, graceful bool, identity string) error { - if err := sendProcessSignal(target, syscall.SIGTERM, identity); err != nil { + outcome, err := sendProcessSignal(target, syscall.SIGTERM, identity) + if err != nil { return fmt.Errorf("send SIGTERM to process target %d: %w", target, err) } + if outcome == targetGone { + return nil + } if graceful { time.Sleep(2 * time.Second) } @@ -317,21 +328,44 @@ func signalProcessTree(target int, graceful bool, identity string) error { if !matches { return nil } - if err := sendProcessSignal(target, syscall.SIGKILL, identity); err != nil { + if _, err := sendProcessSignal(target, syscall.SIGKILL, identity); err != nil { return fmt.Errorf("send SIGKILL to process target %d: %w", target, err) } return nil } -func sendProcessSignal(target int, sig syscall.Signal, identity string) error { - err := syscall.Kill(target, sig) - if err == nil || isProcessGone(err) { - return nil - } - if target < 0 && errors.Is(err, syscall.EPERM) { - return confirmGroupEPERM(-target, identity, err, processGroupMatchesIdentity) +func sendProcessSignal( + target int, + sig syscall.Signal, + identity string, +) (signalOutcome, error) { + return classifySignalResult( + target, + syscall.Kill(target, sig), + identity, + processGroupMatchesIdentity, + ) +} + +func classifySignalResult( + target int, + killErr error, + identity string, + matcher func(int, string) (bool, error), +) (signalOutcome, error) { + switch { + case killErr == nil: + return signalSent, nil + case isProcessGone(killErr): + return targetGone, nil + case target < 0 && errors.Is(killErr, syscall.EPERM): + if err := confirmGroupEPERM(-target, identity, killErr, matcher); err != nil { + return signalSent, err + } + return targetGone, nil + default: + return signalSent, killErr } - return err } func confirmGroupEPERM( diff --git a/pkg/command/process_test.go b/pkg/command/process_test.go index 5fb9ff8b4f..852d3e50ae 100644 --- a/pkg/command/process_test.go +++ b/pkg/command/process_test.go @@ -422,3 +422,97 @@ func TestConfirmGroupEPERMKeepsLiveOrUnverifiedGroupErrors(t *testing.T) { }) } } + +var ( + errSignalUnexpected = errors.New("unexpected") + errSignalVerify = errors.New("enumerate processes") +) + +type groupMatcher func(int, string) (bool, error) + +func matchesLiveMember(int, string) (bool, error) { return true, nil } +func matchesNoMember(int, string) (bool, error) { return false, nil } +func matchFails(int, string) (bool, error) { return false, errSignalVerify } + +type signalResultCase struct { + name string + target int + killErr error + identity string + matcher groupMatcher + want signalOutcome + wantErrs []error +} + +func groupCase(name string, killErr error, matcher groupMatcher) signalResultCase { + return signalResultCase{ + name: name, + target: -42, + killErr: killErr, + identity: testSavedIdentity, + matcher: matcher, + } +} + +func (c signalResultCase) onPositiveTarget() signalResultCase { + c.target = 42 + return c +} + +func (c signalResultCase) expect(want signalOutcome, wantErrs ...error) signalResultCase { + c.want = want + c.wantErrs = wantErrs + return c +} + +func signalResultCases() []signalResultCase { + return []signalResultCase{ + {name: "signal delivered", target: 42, want: signalSent}, + groupCase("positive target gone", syscall.ESRCH, nil). + onPositiveTarget().expect(targetGone), + groupCase("group gone", syscall.ESRCH, nil).expect(targetGone), + groupCase("group EPERM without live member is gone", syscall.EPERM, matchesNoMember). + expect(targetGone), + groupCase("group EPERM with live member keeps error", syscall.EPERM, matchesLiveMember). + expect(signalSent, syscall.EPERM), + groupCase("group EPERM with failed verification keeps both", syscall.EPERM, matchFails). + expect(signalSent, syscall.EPERM, errSignalVerify), + groupCase("positive target EPERM is never confirmed", syscall.EPERM, matchesNoMember). + onPositiveTarget().expect(signalSent, syscall.EPERM), + groupCase("unexpected error is returned", errSignalUnexpected, matchesNoMember). + expect(signalSent, errSignalUnexpected), + } +} + +func TestClassifySignalResult(t *testing.T) { + for _, tc := range signalResultCases() { + t.Run(tc.name, func(t *testing.T) { + got, err := classifySignalResult(tc.target, tc.killErr, tc.identity, tc.matcher) + if got != tc.want { + t.Fatalf("outcome = %v, want %v", got, tc.want) + } + if len(tc.wantErrs) == 0 && err != nil { + t.Fatalf("err = %v, want nil", err) + } + for _, want := range tc.wantErrs { + if !errors.Is(err, want) { + t.Fatalf("err = %v, want errors.Is %v", err, want) + } + } + }) + } +} + +func TestSignalProcessTreeReturnsImmediatelyWhenTargetAlreadyGone(t *testing.T) { + cmd := exec.Command("true") + if err := cmd.Run(); err != nil { + t.Fatalf("run short-lived process: %v", err) + } + start := time.Now() + if err := signalProcessTree(cmd.Process.Pid, true, testSavedIdentity); err != nil { + t.Fatalf("signalProcessTree for exited process: %v", err) + } + if elapsed := time.Since(start); elapsed >= time.Second { + t.Fatalf("signalProcessTree took %v for an already-exited target", elapsed) + } +} From c326b73ffdb7830fa8ad1f54991b2f1607ab22fc Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 14 Oct 2026 11:43:42 -0600 Subject: [PATCH 7/8] test(command): cover darwin zombie leader with live child Gate the leader on stdin so identity capture and child readiness are deterministic, leave the leader unreaped, and assert the group stays matched and the child is terminated. Add a stale-identity case and stop pinning the exact kernel errno in the zombie-only test. --- pkg/command/process_identity_darwin_test.go | 98 +++++++++++++++++++-- 1 file changed, 90 insertions(+), 8 deletions(-) diff --git a/pkg/command/process_identity_darwin_test.go b/pkg/command/process_identity_darwin_test.go index e4adb93578..27be95cd52 100644 --- a/pkg/command/process_identity_darwin_test.go +++ b/pkg/command/process_identity_darwin_test.go @@ -3,7 +3,11 @@ package command import ( + "os" "os/exec" + "path/filepath" + "strconv" + "strings" "syscall" "testing" "time" @@ -13,25 +17,42 @@ import ( "golang.org/x/sys/unix" ) -func startZombieGroupLeader(t *testing.T) (pid int, identity string) { +const ( + darwinTestPollTimeout = 5 * time.Second + darwinTestPollInterval = 10 * time.Millisecond +) + +func startGatedGroupLeader(t *testing.T, script string) (pid int, release func(), arg string) { t.Helper() - cmd := exec.Command("sh", "-c", "read _") + arg = filepath.Join(t.TempDir(), "ready") + // #nosec G204 -- the fixed shell script and all paths are test-controlled. + cmd := exec.Command("sh", "-c", script, "sh", arg) cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} stdin, err := cmd.StdinPipe() require.NoError(t, err) require.NoError(t, cmd.Start()) + pid = cmd.Process.Pid t.Cleanup(func() { - _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) + _ = syscall.Kill(-pid, syscall.SIGKILL) _ = cmd.Wait() }) - pid = cmd.Process.Pid - identity = processTreeIdentityForTest(t, pid) + return pid, func() { require.NoError(t, stdin.Close()) }, arg +} - require.NoError(t, stdin.Close()) +func waitForZombie(t *testing.T, pid int) { + t.Helper() require.Eventually(t, func() bool { proc, err := unix.SysctlKinfoProc("kern.proc.pid", pid) return err == nil && int(proc.Proc.P_pid) == pid && darwinProcessIsZombie(*proc) - }, 5*time.Second, 10*time.Millisecond, "leader did not become a zombie") + }, darwinTestPollTimeout, darwinTestPollInterval, "process %d did not become a zombie", pid) +} + +func startZombieGroupLeader(t *testing.T) (pid int, identity string) { + t.Helper() + pid, release, _ := startGatedGroupLeader(t, `read _`) + identity = processTreeIdentityForTest(t, pid) + release() + waitForZombie(t, pid) return pid, identity } @@ -54,8 +75,69 @@ func TestDarwinZombieLeaderIsNotLiveGroupMember(t *testing.T) { func TestDarwinKillWorkerTreeOnZombieOnlyGroupSucceeds(t *testing.T) { pid, identity := startZombieGroupLeader(t) - require.ErrorIs(t, syscall.Kill(-pid, syscall.SIGTERM), syscall.EPERM) require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", identity)) + waitForZombie(t, pid) +} + +func TestDarwinZombieLeaderWithLiveChildKeepsGroupMatched(t *testing.T) { + pid, release, readyFile := startGatedGroupLeader(t, `sleep 30 & echo $! > "$1"; read _`) + childPID := waitForChildPID(t, readyFile) + childGroup, err := unix.Getpgid(childPID) + require.NoError(t, err) + require.Equal(t, pid, childGroup, "child must share the leader's process group") + identity := processTreeIdentityForTest(t, pid) + + release() + waitForZombie(t, pid) + childLive, err := processIsLive(childPID) + require.NoError(t, err) + require.True(t, childLive, "child must outlive the zombie leader") + + matches, err := processGroupMatchesIdentity(pid, identity) + require.NoError(t, err) + assert.True(t, matches, "live child must keep the group matched") + living, err := processGroupHasLivingMembers(pid) + require.NoError(t, err) + assert.True(t, living, "live child must count as a living member") + + require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", identity)) + require.Eventually(t, func() bool { + live, err := processIsLive(childPID) + return err == nil && !live + }, darwinTestPollTimeout, darwinTestPollInterval, "child %d survived termination", childPID) +} + +func TestDarwinZombieLeaderWithLiveChildRejectsStaleIdentity(t *testing.T) { + pid, release, readyFile := startGatedGroupLeader(t, `sleep 30 & echo $! > "$1"; read _`) + childPID := waitForChildPID(t, readyFile) + identity := processTreeIdentityForTest(t, pid) + stale := identity[:strings.LastIndex(identity, ":")] + ":0" + + release() + waitForZombie(t, pid) + + matches, err := processGroupMatchesIdentity(pid, stale) + require.NoError(t, err) + assert.False(t, matches, "stale identity matched a group") + require.NoError(t, killWorkerTreeWithIdentity(pid, "detached-task", stale)) + childLive, err := processIsLive(childPID) + require.NoError(t, err) + assert.True(t, childLive, "stale identity must not terminate the child") +} + +func waitForChildPID(t *testing.T, readyFile string) int { + t.Helper() + var childPID int + require.Eventually(t, func() bool { + // #nosec G304 -- this test reads only its own temporary readiness file. + data, err := os.ReadFile(readyFile) + if err != nil || !strings.HasSuffix(string(data), "\n") { + return false + } + childPID, err = strconv.Atoi(strings.TrimSpace(string(data))) + return err == nil + }, darwinTestPollTimeout, darwinTestPollInterval, "child did not report its PID") + return childPID } func TestDarwinProcessGroupHasLivingMembersSeesLiveChild(t *testing.T) { From 1f43a2493cb4eca56cf8b09ff9ce1c5786358ef9 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Fri, 16 Oct 2026 16:14:48 -0600 Subject: [PATCH 8/8] test(command): use an absent PID for the already-gone fast-path test A reaped PID can be reused, which would signal an unrelated process and trip the grace wait. Use a PID that cannot name a live process. --- pkg/command/process_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pkg/command/process_test.go b/pkg/command/process_test.go index 852d3e50ae..ab8948a3c9 100644 --- a/pkg/command/process_test.go +++ b/pkg/command/process_test.go @@ -504,12 +504,9 @@ func TestClassifySignalResult(t *testing.T) { } func TestSignalProcessTreeReturnsImmediatelyWhenTargetAlreadyGone(t *testing.T) { - cmd := exec.Command("true") - if err := cmd.Run(); err != nil { - t.Fatalf("run short-lived process: %v", err) - } + const absentPID = int(^uint32(0) >> 1) start := time.Now() - if err := signalProcessTree(cmd.Process.Pid, true, testSavedIdentity); err != nil { + if err := signalProcessTree(absentPID, true, testSavedIdentity); err != nil { t.Fatalf("signalProcessTree for exited process: %v", err) } if elapsed := time.Since(start); elapsed >= time.Second {