From e7810febd6d0e2bb0adc5f26ac07fe5187128db2 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 13 Nov 2025 14:37:28 +0800 Subject: [PATCH] fix: Fix the issue of abnormal context in the cmd method (#10939) --- agent/utils/cmd/cmdx.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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") } }