Skip to content

Commit 45f70a8

Browse files
authored
Merge pull request #456 from ZYecho/fix_timeout
fix: fix script timeout can't work
2 parents c2d7a7b + 4f68b25 commit 45f70a8

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

pkg/custompluginmonitor/plugin/plugin.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
147147
}
148148
defer cancel()
149149

150-
cmd := exec.CommandContext(ctx, rule.Path, rule.Args...)
150+
// create a process group
151+
sysProcAttr := &syscall.SysProcAttr{
152+
Setpgid: true,
153+
}
154+
cmd := exec.Command(rule.Path, rule.Args...)
155+
cmd.SysProcAttr = sysProcAttr
151156

152157
stdoutPipe, err := cmd.StdoutPipe()
153158
if err != nil {
@@ -164,6 +169,26 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
164169
return cpmtypes.Unknown, "Error in starting plugin. Please check the error log"
165170
}
166171

172+
waitChan := make(chan struct{})
173+
defer close(waitChan)
174+
175+
go func() {
176+
select {
177+
case <-ctx.Done():
178+
glog.Errorf("Error in running plugin timeout %q", rule.Path)
179+
if cmd.Process == nil || cmd.Process.Pid == 0 {
180+
glog.Errorf("Error in cmd.Process check %q", rule.Path)
181+
break
182+
}
183+
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
184+
if err != nil {
185+
glog.Errorf("Error in kill process %d, %v", cmd.Process.Pid, err)
186+
}
187+
case <-waitChan:
188+
return
189+
}
190+
}()
191+
167192
var (
168193
wg sync.WaitGroup
169194
stdout []byte

0 commit comments

Comments
 (0)