fix: fix script timeout can't work

Signed-off-by: zhangyue <huaihuan.zy@alibaba-inc.com>
This commit is contained in:
zhangyue
2021-01-13 20:53:25 +08:00
parent 8a41d4abe3
commit 4f68b251ac
+26 -1
View File
@@ -148,7 +148,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 {
@@ -165,6 +170,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