diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/blame.go | 1 | ||||
-rw-r--r-- | modules/git/command.go | 1 | ||||
-rw-r--r-- | modules/markup/external/external.go | 2 | ||||
-rw-r--r-- | modules/process/manager_exec.go | 1 | ||||
-rw-r--r-- | modules/process/manager_unix.go | 18 | ||||
-rw-r--r-- | modules/process/manager_windows.go | 16 | ||||
-rw-r--r-- | modules/ssh/ssh.go | 2 |
7 files changed, 41 insertions, 0 deletions
diff --git a/modules/git/blame.go b/modules/git/blame.go index 40e3d4e885..1653ecbf85 100644 --- a/modules/git/blame.go +++ b/modules/git/blame.go @@ -124,6 +124,7 @@ func createBlameReader(ctx context.Context, dir string, command ...string) (*Bla cmd := exec.CommandContext(ctx, command[0], command[1:]...) cmd.Dir = dir cmd.Stderr = os.Stderr + process.SetSysProcAttribute(cmd) stdout, err := cmd.StdoutPipe() if err != nil { diff --git a/modules/git/command.go b/modules/git/command.go index 3dd12e421e..f6344dbfd1 100644 --- a/modules/git/command.go +++ b/modules/git/command.go @@ -157,6 +157,7 @@ func (c *Command) Run(opts *RunOpts) error { "GIT_NO_REPLACE_OBJECTS=1", ) + process.SetSysProcAttribute(cmd) cmd.Dir = opts.Dir cmd.Stdout = opts.Stdout cmd.Stderr = opts.Stderr diff --git a/modules/markup/external/external.go b/modules/markup/external/external.go index 4fdd4315bc..a587abcc3b 100644 --- a/modules/markup/external/external.go +++ b/modules/markup/external/external.go @@ -124,6 +124,8 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io. cmd.Stdin = input } cmd.Stdout = output + process.SetSysProcAttribute(cmd) + if err := cmd.Run(); err != nil { return fmt.Errorf("%s render run command %s %v failed: %v", p.Name(), commands[0], args, err) } diff --git a/modules/process/manager_exec.go b/modules/process/manager_exec.go index 61ddae646f..77e3d3193a 100644 --- a/modules/process/manager_exec.go +++ b/modules/process/manager_exec.go @@ -58,6 +58,7 @@ func (pm *Manager) ExecDirEnvStdIn(ctx context.Context, timeout time.Duration, d if stdIn != nil { cmd.Stdin = stdIn } + SetSysProcAttribute(cmd) if err := cmd.Start(); err != nil { return "", "", err diff --git a/modules/process/manager_unix.go b/modules/process/manager_unix.go new file mode 100644 index 0000000000..1e7c77fdbf --- /dev/null +++ b/modules/process/manager_unix.go @@ -0,0 +1,18 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +//go:build !windows + +package process + +import ( + "os/exec" + "syscall" +) + +// SetSysProcAttribute sets the common SysProcAttrs for commands +func SetSysProcAttribute(cmd *exec.Cmd) { + // When Gitea runs SubProcessA -> SubProcessB and SubProcessA gets killed by context timeout, use setpgid to make sure the sub processes can be reaped instead of leaving defunct(zombie) processes. + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} +} diff --git a/modules/process/manager_windows.go b/modules/process/manager_windows.go new file mode 100644 index 0000000000..35f66d9fa5 --- /dev/null +++ b/modules/process/manager_windows.go @@ -0,0 +1,16 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +//go:build windows + +package process + +import ( + "os/exec" +) + +// SetSysProcAttribute sets the common SysProcAttrs for commands +func SetSysProcAttribute(cmd *exec.Cmd) { + // Do nothing +} diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index fe3561cefa..a240c01319 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -102,6 +102,8 @@ func sessionHandler(session ssh.Session) { } defer stdin.Close() + process.SetSysProcAttribute(cmd) + wg := &sync.WaitGroup{} wg.Add(2) |