fix timeout not working on linux/mac

This commit is contained in:
wh1te909 2023-08-01 05:09:48 +00:00
parent 830f418888
commit 6c1fa2f061

View File

@ -254,7 +254,7 @@ type CmdOptions struct {
func (a *Agent) NewCMDOpts() *CmdOptions {
return &CmdOptions{
Shell: "/bin/bash",
Timeout: 30,
Timeout: 60,
}
}
@ -322,38 +322,46 @@ func (a *Agent) CmdV2(c *CmdOptions) CmdStatus {
}
}()
statusChan := make(chan gocmd.Status, 1)
// workaround for https://github.com/golang/go/issues/22315
go func() {
for i := 0; i < 5; i++ {
<-envCmd.Start()
<-doneChan
status := envCmd.Status()
if errors.Is(status.Error, syscall.ETXTBSY) {
finalStatus := <-envCmd.Start()
if errors.Is(finalStatus.Error, syscall.ETXTBSY) {
a.Logger.Errorln("CmdV2 syscall.ETXTBSY, retrying...")
time.Sleep(500 * time.Millisecond)
} else {
break
continue
}
}
go func() {
select {
case <-doneChan:
statusChan <- finalStatus
return
}
}()
var finalStatus gocmd.Status
select {
case <-ctx.Done():
a.Logger.Debugf("Command timed out after %d seconds\n", c.Timeout)
pid := envCmd.Status().PID
a.Logger.Debugln("Killing process with PID", pid)
KillProc(int32(pid))
finalStatus.Exit = 98
ret := CmdStatus{
Status: finalStatus,
Stdout: CleanString(stdoutBuf.String()),
Stderr: fmt.Sprintf("%s\nTimed out after %d seconds", CleanString(stderrBuf.String()), c.Timeout),
}
a.Logger.Debugf("%+v\n", ret)
return ret
case finalStatus = <-statusChan:
// done
}
}()
// Wait for goroutine to print everything
<-doneChan
ret := CmdStatus{
Status: envCmd.Status(),
Status: finalStatus,
Stdout: CleanString(stdoutBuf.String()),
Stderr: CleanString(stderrBuf.String()),
}