diff --git a/agent/utils/cmd/cmdx.go b/agent/utils/cmd/cmdx.go index 4303c1874..5923280fa 100644 --- a/agent/utils/cmd/cmdx.go +++ b/agent/utils/cmd/cmdx.go @@ -94,21 +94,23 @@ func (c *CommandHelper) RunWithStdoutBashCf(command string, arg ...interface{}) func (c *CommandHelper) run(name string, arg ...string) (string, error) { var cmd *exec.Cmd + var newContext context.Context var cancel context.CancelFunc if c.timeout != 0 { if c.context == nil { - c.context, cancel = context.WithTimeout(context.Background(), c.timeout) + newContext, cancel = context.WithTimeout(context.Background(), c.timeout) defer cancel() } else { - c.context, cancel = context.WithTimeout(c.context, c.timeout) + newContext, cancel = context.WithTimeout(c.context, c.timeout) defer cancel() } - cmd = exec.CommandContext(c.context, name, arg...) + cmd = exec.CommandContext(newContext, name, arg...) } else { if c.context == nil { cmd = exec.Command(name, arg...) } else { + newContext = c.context cmd = exec.CommandContext(c.context, name, arg...) } } @@ -148,7 +150,7 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) { customWriter.Flush() } if c.timeout != 0 { - if c.context != nil && errors.Is(c.context.Err(), context.DeadlineExceeded) { + if newContext != nil && errors.Is(newContext.Err(), context.DeadlineExceeded) { return "", buserr.New("ErrCmdTimeout") } }