File tree Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ import (
13
13
// to indicated that the process is a child as opposed to the reaper.
14
14
// Since we are forkexec'ing we need to be able to differentiate between
15
15
// the two to avoid fork bombing ourselves.
16
- const agentEnvMark = "CODER_AGENT "
16
+ const agentEnvMark = "CODER_REAPER_AGENT "
17
17
18
18
// IsChild returns true if we're the forked process.
19
19
func IsChild () bool {
@@ -29,6 +29,8 @@ func IsInitProcess() bool {
29
29
// complications with spawning `exec.Commands` in the same process that
30
30
// is reaping, we forkexec a child process. This prevents a race between
31
31
// the reaper and an exec.Command waiting for its process to complete.
32
+ // The provided 'pids' channel may be nil if the caller does not care about the
33
+ // reaped children PIDs.
32
34
func ForkReap (pids reap.PidCh ) error {
33
35
// Check if the process is the parent or the child.
34
36
// If it's the child we want to skip attempting to reap.
Original file line number Diff line number Diff line change 1
1
package reaper_test
2
2
3
3
import (
4
+ "fmt"
4
5
"os/exec"
5
6
"testing"
6
7
"time"
@@ -19,6 +20,9 @@ func TestReap(t *testing.T) {
19
20
t .Skip ("I'm a child!" )
20
21
}
21
22
23
+ // OK checks that's the reaper is successfully reaping
24
+ // exited processes and passing the PIDs through the shared
25
+ // channel.
22
26
t .Run ("OK" , func (t * testing.T ) {
23
27
pids := make (reap.PidCh , 1 )
24
28
err := reaper .ForkReap (pids )
@@ -41,11 +45,14 @@ func TestReap(t *testing.T) {
41
45
expectedPIDs := []int {cmd .Process .Pid , cmd2 .Process .Pid }
42
46
43
47
deadline := time .NewTimer (time .Second * 5 )
44
- select {
45
- case <- deadline .C :
46
- t .Fatalf ("Timed out waiting for process" )
47
- case pid := <- pids :
48
- require .Contains (t , expectedPIDs , pid )
48
+ for i := 0 ; i < len (expectedPIDs ); i ++ {
49
+ select {
50
+ case <- deadline .C :
51
+ t .Fatalf ("Timed out waiting for process" )
52
+ case pid := <- pids :
53
+ fmt .Println ("pid: " , pid )
54
+ require .Contains (t , expectedPIDs , pid )
55
+ }
49
56
}
50
57
})
51
58
}
You can’t perform that action at this time.
0 commit comments