fix timeout not working on linux/mac
This commit is contained in:
parent
830f418888
commit
6c1fa2f061
@ -254,7 +254,7 @@ type CmdOptions struct {
|
|||||||
func (a *Agent) NewCMDOpts() *CmdOptions {
|
func (a *Agent) NewCMDOpts() *CmdOptions {
|
||||||
return &CmdOptions{
|
return &CmdOptions{
|
||||||
Shell: "/bin/bash",
|
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
|
// workaround for https://github.com/golang/go/issues/22315
|
||||||
|
go func() {
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
<-envCmd.Start()
|
finalStatus := <-envCmd.Start()
|
||||||
|
if errors.Is(finalStatus.Error, syscall.ETXTBSY) {
|
||||||
<-doneChan
|
|
||||||
|
|
||||||
status := envCmd.Status()
|
|
||||||
|
|
||||||
if errors.Is(status.Error, syscall.ETXTBSY) {
|
|
||||||
a.Logger.Errorln("CmdV2 syscall.ETXTBSY, retrying...")
|
a.Logger.Errorln("CmdV2 syscall.ETXTBSY, retrying...")
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
} else {
|
continue
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
statusChan <- finalStatus
|
||||||
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-doneChan:
|
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var finalStatus gocmd.Status
|
||||||
|
|
||||||
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
a.Logger.Debugf("Command timed out after %d seconds\n", c.Timeout)
|
a.Logger.Debugf("Command timed out after %d seconds\n", c.Timeout)
|
||||||
pid := envCmd.Status().PID
|
pid := envCmd.Status().PID
|
||||||
a.Logger.Debugln("Killing process with PID", pid)
|
a.Logger.Debugln("Killing process with PID", pid)
|
||||||
KillProc(int32(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
|
// Wait for goroutine to print everything
|
||||||
<-doneChan
|
<-doneChan
|
||||||
|
|
||||||
ret := CmdStatus{
|
ret := CmdStatus{
|
||||||
Status: envCmd.Status(),
|
Status: finalStatus,
|
||||||
Stdout: CleanString(stdoutBuf.String()),
|
Stdout: CleanString(stdoutBuf.String()),
|
||||||
Stderr: CleanString(stderrBuf.String()),
|
Stderr: CleanString(stderrBuf.String()),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user