fix: preserve VM task timeout cleanup and terminate command groups (#14008)

This commit is contained in:
ssongliu
2026-10-09 17:45:41 +08:00
committed by GitHub
parent f22df4b29c
commit c17f055e06
3 changed files with 37 additions and 4 deletions
+9
View File
@@ -87,6 +87,7 @@ const (
TaskScopeCronjob = "Cronjob"
TaskScopeClam = "Clam"
TaskScopeSystem = "System"
TaskScopeVm = "VirtualMachine"
TaskScopeFirewall = "Firewall"
TaskScopeAppStore = "AppStore"
TaskScopeSnapshot = "Snapshot"
@@ -258,7 +259,15 @@ func (s *SubTask) Execute() error {
case <-ctx.Done():
s.RootTask.Log(i18n.GetWithName("TaskTimeout", subTaskName))
err = errors.New("timeout!")
if s.RootTask.Task.Type == TaskScopeVm {
<-done
}
case err = <-done:
if s.RootTask.Task.Type == TaskScopeVm && ctx.Err() != nil {
s.RootTask.Log(i18n.GetWithName("TaskTimeout", subTaskName))
err = errors.New("timeout!")
break
}
if err != nil {
s.RootTask.Log(i18n.GetWithNameAndErr("SubTaskFailed", subTaskName, err))
if err.Error() == i18n.GetMsgByKey("ErrShutDown") {
+13
View File
@@ -251,6 +251,10 @@ func (c *CommandHelper) buildPipeCommands(ctx context.Context, commands []PipeCo
cmdItem.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cmdItem.Cancel = func() error {
_ = syscall.Kill(-cmdItem.Process.Pid, syscall.SIGKILL)
return cmdItem.Process.Kill()
}
cmds = append(cmds, cmdItem)
}
return cmds
@@ -335,6 +339,12 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if newContext != nil {
cmd.Cancel = func() error {
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return cmd.Process.Kill()
}
}
customWriter := &CustomWriter{taskItem: c.taskItem}
var stdout, stderr bytes.Buffer
@@ -398,6 +408,9 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
}()
select {
case err := <-done:
if newContext != nil && newContext.Err() != nil {
return "", c.pipeResultErr(newContext, err)
}
if err != nil {
return handleErr(&stdout, &stderr, c.IgnoreExist1, err)
}
+15 -4
View File
@@ -230,6 +230,10 @@ func (c *CommandHelper) buildPipeCommands(ctx context.Context, commands []PipeCo
cmdItem.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cmdItem.Cancel = func() error {
_ = syscall.Kill(-cmdItem.Process.Pid, syscall.SIGKILL)
return cmdItem.Process.Kill()
}
cmds = append(cmds, cmdItem)
}
return cmds
@@ -305,6 +309,12 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if newContext != nil {
cmd.Cancel = func() error {
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return cmd.Process.Kill()
}
}
customWriter := &CustomWriter{taskItem: c.taskItem}
var stdout, stderr bytes.Buffer
@@ -357,11 +367,12 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
}()
select {
case err := <-done:
if c.preserveErrorCause && newContext != nil && newContext.Err() != nil {
if cmd.Process != nil && cmd.Process.Pid > 0 {
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if newContext != nil && newContext.Err() != nil {
resultErr := c.pipeResultErr(newContext, err)
if c.preserveErrorCause {
resultErr = &commandError{message: resultErr.Error(), cause: newContext.Err()}
}
return "", newContext.Err()
return "", resultErr
}
if err != nil {
out, resultErr := handleErr(&stdout, &stderr, c.IgnoreExist1, err)