File tree 1 file changed +12
-3
lines changed
1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -575,6 +575,15 @@ func (c *kcpServer) Run(opts ...RunOption) error {
575
575
// NOTE: do not use exec.CommandContext here. That method issues a SIGKILL when the context is done, and we
576
576
// want to issue SIGTERM instead, to give the server a chance to shut down cleanly.
577
577
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
+
578
587
logFile , err := os .Create (filepath .Join (c .artifactDir , "kcp.log" ))
579
588
if err != nil {
580
589
cleanup ()
@@ -607,9 +616,9 @@ func (c *kcpServer) Run(opts ...RunOption) error {
607
616
}
608
617
609
618
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 {
613
622
c .t .Errorf ("Saw an error trying to kill `kcp`: %v" , err )
614
623
}
615
624
})
You can’t perform that action at this time.
0 commit comments