Merge pull request #456 from ZYecho/fix_timeout

fix: fix script timeout can't work
This commit is contained in:
Kubernetes Prow Robot
2021-01-19 19:01:58 -08:00
committed by GitHub
+26 -1
View File
@@ -147,7 +147,12 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
}
defer cancel()
cmd := exec.CommandContext(ctx, rule.Path, rule.Args...)
// create a process group
sysProcAttr := &syscall.SysProcAttr{
Setpgid: true,
}
cmd := exec.Command(rule.Path, rule.Args...)
cmd.SysProcAttr = sysProcAttr
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
@@ -164,6 +169,26 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
return cpmtypes.Unknown, "Error in starting plugin. Please check the error log"
}
waitChan := make(chan struct{})
defer close(waitChan)
go func() {
select {
case <-ctx.Done():
glog.Errorf("Error in running plugin timeout %q", rule.Path)
if cmd.Process == nil || cmd.Process.Pid == 0 {
glog.Errorf("Error in cmd.Process check %q", rule.Path)
break
}
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
glog.Errorf("Error in kill process %d, %v", cmd.Process.Pid, err)
}
case <-waitChan:
return
}
}()
var (
wg sync.WaitGroup
stdout []byte