|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2026 The Dapr Authors |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "os/exec" |
| 20 | + "syscall" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +// TestKillProcessGroup_KillsEntireGroup verifies that killProcessGroup terminates all |
| 29 | +// processes in the group, not just the leader. A second process is explicitly placed |
| 30 | +// into the leader's process group using SysProcAttr.Pgid; both must be gone after |
| 31 | +// killProcessGroup returns. |
| 32 | +func TestKillProcessGroup_KillsEntireGroup(t *testing.T) { |
| 33 | + leader := exec.Command("sleep", "100") |
| 34 | + leader.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 35 | + require.NoError(t, leader.Start()) |
| 36 | + |
| 37 | + // Explicitly place a second process in the same process group as the leader. |
| 38 | + member := exec.Command("sleep", "100") |
| 39 | + member.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: leader.Process.Pid} |
| 40 | + require.NoError(t, member.Start()) |
| 41 | + |
| 42 | + // Wait goroutines reap the processes once killed, so Kill(-pgid, 0) eventually |
| 43 | + // returns ESRCH rather than seeing zombies that still count as group members. |
| 44 | + go func() { _ = leader.Wait() }() |
| 45 | + go func() { _ = member.Wait() }() |
| 46 | + |
| 47 | + require.NoError(t, killProcessGroup(leader.Process)) |
| 48 | + |
| 49 | + assert.Eventually(t, func() bool { |
| 50 | + return syscall.Kill(-leader.Process.Pid, 0) == syscall.ESRCH |
| 51 | + }, 2*time.Second, 50*time.Millisecond, "process group should be gone after killProcessGroup") |
| 52 | +} |
| 53 | + |
| 54 | +// TestKillProcessGroup_AlreadyGone verifies that killProcessGroup returns nil when |
| 55 | +// the process has already exited cleanly before kill is attempted. |
| 56 | +func TestKillProcessGroup_AlreadyGone(t *testing.T) { |
| 57 | + cmd := exec.Command("true") |
| 58 | + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 59 | + require.NoError(t, cmd.Start()) |
| 60 | + proc := cmd.Process |
| 61 | + require.NoError(t, cmd.Wait()) |
| 62 | + |
| 63 | + assert.NoError(t, killProcessGroup(proc)) |
| 64 | +} |
| 65 | + |
| 66 | +// TestKillProcessGroup_LeaderExitedGroupAlive verifies that killProcessGroup still |
| 67 | +// kills remaining group members when the leader has already exited — the typical |
| 68 | +// `go run` scenario where the wrapper process exits but the compiled binary lives on |
| 69 | +// in the same process group. |
| 70 | +func TestKillProcessGroup_LeaderExitedGroupAlive(t *testing.T) { |
| 71 | + leader := exec.Command("sleep", "100") |
| 72 | + leader.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 73 | + require.NoError(t, leader.Start()) |
| 74 | + leaderPGID := leader.Process.Pid |
| 75 | + |
| 76 | + // Place a second process in the same group as the leader. |
| 77 | + member := exec.Command("sleep", "100") |
| 78 | + member.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: leaderPGID} |
| 79 | + require.NoError(t, member.Start()) |
| 80 | + |
| 81 | + // Kill only the group leader; member stays alive in the same group. |
| 82 | + require.NoError(t, leader.Process.Kill()) |
| 83 | + _ = leader.Wait() // reap immediately so it doesn't linger as a zombie |
| 84 | + |
| 85 | + // Confirm the group still has a live member before we call killProcessGroup. |
| 86 | + require.NoError(t, syscall.Kill(-leaderPGID, 0), "member should still be alive in the group") |
| 87 | + |
| 88 | + // killProcessGroup falls back to process.Pid as PGID since Getpgid returns ESRCH |
| 89 | + // for the dead leader, then signals and terminates the remaining member. |
| 90 | + go func() { _ = member.Wait() }() |
| 91 | + |
| 92 | + require.NoError(t, killProcessGroup(leader.Process)) |
| 93 | + |
| 94 | + assert.Eventually(t, func() bool { |
| 95 | + return syscall.Kill(-leaderPGID, 0) == syscall.ESRCH |
| 96 | + }, 2*time.Second, 50*time.Millisecond, "process group should be gone after leader exits") |
| 97 | +} |
0 commit comments