Skip to content

Commit 1cfbe44

Browse files
committed
e2e: support terminating "go run kcp start"
Signed-off-by: Andy Goldstein <[email protected]>
1 parent a9c50f4 commit 1cfbe44

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

test/e2e/framework/kcp.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,15 @@ func (c *kcpServer) Run(opts ...RunOption) error {
575575
// NOTE: do not use exec.CommandContext here. That method issues a SIGKILL when the context is done, and we
576576
// want to issue SIGTERM instead, to give the server a chance to shut down cleanly.
577577
cmd := exec.Command(commandLine[0], commandLine[1:]...)
578+
579+
// Create a new process group for the child/forked process (which is either 'go run ...' or just 'kcp
580+
// ...'). This is necessary so the SIGTERM we send to terminate the kcp server works even with the
581+
// 'go run' variant - we have to work around this issue: https://github.com/golang/go/issues/40467.
582+
// Thanks to
583+
// https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773 for
584+
// the idea!
585+
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
586+
578587
logFile, err := os.Create(filepath.Join(c.artifactDir, "kcp.log"))
579588
if err != nil {
580589
cleanup()
@@ -607,9 +616,9 @@ func (c *kcpServer) Run(opts ...RunOption) error {
607616
}
608617

609618
c.t.Cleanup(func() {
610-
// Ensure child process is killed on cleanup
611-
err := cmd.Process.Signal(syscall.SIGTERM)
612-
if err != nil {
619+
// Ensure child process is killed on cleanup - send the negative of the pid, which is the process group id.
620+
// See https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773 for details.
621+
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM); err != nil {
613622
c.t.Errorf("Saw an error trying to kill `kcp`: %v", err)
614623
}
615624
})

0 commit comments

Comments
 (0)