Repository navigation
fix(command): stop darwin EPERM on zombie-only process groups #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45857a3
fix(command): exclude zombies from darwin process group identity
skevetter f4f1774
fix(command): confirm EPERM against saved identity when killing groups
skevetter 877373f
test(command): synchronize group fixture and cover darwin zombie groups
skevetter a506b84
style(command): split linux living-member scan to satisfy cyclop
skevetter dc442b6
fix(command): tolerate unreadable proc entries when scanning group li…
skevetter b022205
fix(command): skip grace wait when SIGTERM target is already gone
skevetter c326b73
test(command): cover darwin zombie leader with live child
skevetter 1f43a24
test(command): use an absent PID for the already-gone fast-path test
skevetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| //go:build darwin | ||
|
|
||
| package command | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| const ( | ||
| darwinTestPollTimeout = 5 * time.Second | ||
| darwinTestPollInterval = 10 * time.Millisecond | ||
| ) | ||
|
|
||
| func startGatedGroupLeader(t *testing.T, script string) (pid int, release func(), arg string) { | ||
| t.Helper() | ||
| 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(-pid, syscall.SIGKILL) | ||
| _ = cmd.Wait() | ||
| }) | ||
| return pid, func() { require.NoError(t, stdin.Close()) }, arg | ||
| } | ||
|
|
||
| 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) | ||
| }, 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 | ||
| } | ||
|
|
||
| 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.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) { | ||
| 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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.