Set Setpgid on child git processes (#19865)

When Gitea is running as PID 1 git will occassionally orphan child processes leading
to (defunct) processes. This PR simply sets Setpgid to true on these child processes
meaning that these defunct processes will also be correctly reaped.

Fix #19077

Signed-off-by: Andrew Thornton <art27@cantab.net>
tokarchuk/v1.17
zeripath 3 years ago committed by GitHub
parent 085924b1b3
commit 1d04e8641d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      cmd/serv.go
  2. 1
      modules/git/blame.go
  3. 1
      modules/git/command.go
  4. 2
      modules/markup/external/external.go
  5. 1
      modules/process/manager_exec.go
  6. 18
      modules/process/manager_unix.go
  7. 16
      modules/process/manager_windows.go
  8. 2
      modules/ssh/ssh.go
  9. 1
      services/mailer/mailer.go

@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/pprof" "code.gitea.io/gitea/modules/pprof"
"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/process"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/lfs" "code.gitea.io/gitea/services/lfs"
@ -306,6 +307,7 @@ func runServ(c *cli.Context) error {
} }
} }
process.SetSysProcAttribute(gitcmd)
gitcmd.Dir = setting.RepoRootPath gitcmd.Dir = setting.RepoRootPath
gitcmd.Stdout = os.Stdout gitcmd.Stdout = os.Stdout
gitcmd.Stdin = os.Stdin gitcmd.Stdin = os.Stdin

@ -124,6 +124,7 @@ func createBlameReader(ctx context.Context, dir string, command ...string) (*Bla
cmd := exec.CommandContext(ctx, command[0], command[1:]...) cmd := exec.CommandContext(ctx, command[0], command[1:]...)
cmd.Dir = dir cmd.Dir = dir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
process.SetSysProcAttribute(cmd)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {

@ -157,6 +157,7 @@ func (c *Command) Run(opts *RunOpts) error {
"GIT_NO_REPLACE_OBJECTS=1", "GIT_NO_REPLACE_OBJECTS=1",
) )
process.SetSysProcAttribute(cmd)
cmd.Dir = opts.Dir cmd.Dir = opts.Dir
cmd.Stdout = opts.Stdout cmd.Stdout = opts.Stdout
cmd.Stderr = opts.Stderr cmd.Stderr = opts.Stderr

@ -124,6 +124,8 @@ func (p *Renderer) Render(ctx *markup.RenderContext, input io.Reader, output io.
cmd.Stdin = input cmd.Stdin = input
} }
cmd.Stdout = output cmd.Stdout = output
process.SetSysProcAttribute(cmd)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return fmt.Errorf("%s render run command %s %v failed: %v", p.Name(), commands[0], args, err) return fmt.Errorf("%s render run command %s %v failed: %v", p.Name(), commands[0], args, err)
} }

@ -58,6 +58,7 @@ func (pm *Manager) ExecDirEnvStdIn(ctx context.Context, timeout time.Duration, d
if stdIn != nil { if stdIn != nil {
cmd.Stdin = stdIn cmd.Stdin = stdIn
} }
SetSysProcAttribute(cmd)
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
return "", "", err return "", "", err

@ -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}
}

@ -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
}

@ -102,6 +102,8 @@ func sessionHandler(session ssh.Session) {
} }
defer stdin.Close() defer stdin.Close()
process.SetSysProcAttribute(cmd)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(2) wg.Add(2)

@ -281,6 +281,7 @@ func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
if err != nil { if err != nil {
return err return err
} }
process.SetSysProcAttribute(cmd)
if err = cmd.Start(); err != nil { if err = cmd.Start(); err != nil {
_ = pipe.Close() _ = pipe.Close()

Loading…
Cancel
Save