Skip to content

Commit 4f68b25

Browse files
committed
fix: fix script timeout can't work
Signed-off-by: zhangyue <[email protected]>
1 parent 8a41d4a commit 4f68b25

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
@@ -148,7 +148,12 @@ func (p *Plugin) run(rule cpmtypes.CustomRule) (exitStatus cpmtypes.Status, outp
148148
}
149149
defer cancel()
150150

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

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

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

0 commit comments

Comments
 (0)