Skip to content

Commit

Permalink
Set Setpgid on child git processes (go-gitea#19865)
Browse files Browse the repository at this point in the history
Backport go-gitea#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 go-gitea#19077

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Jun 3, 2022
1 parent 704f809 commit b3aa38b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/pprof"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/lfs"

Expand Down Expand Up @@ -247,7 +248,7 @@ func runServ(c *cli.Context) error {
os.Setenv(models.EnvKeyID, fmt.Sprintf("%d", results.KeyID))
os.Setenv(models.EnvAppURL, setting.AppURL)

//LFS token authentication
// LFS token authentication
if verb == lfsAuthenticateVerb {
url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, url.PathEscape(results.OwnerName), url.PathEscape(results.RepoName))

Expand Down Expand Up @@ -306,6 +307,7 @@ func runServ(c *cli.Context) error {
}
}

process.SetSysProcAttribute(gitcmd)
gitcmd.Dir = setting.RepoRootPath
gitcmd.Stdout = os.Stdout
gitcmd.Stdin = os.Stdin
Expand Down
1 change: 1 addition & 0 deletions modules/git/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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 {
Expand Down
1 change: 1 addition & 0 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (c *Command) RunWithContext(rc *RunContext) error {
if goVersionLessThan115 {
cmd.Env = append(cmd.Env, "GODEBUG=asyncpreemptoff=1")
}
process.SetSysProcAttribute(cmd)
cmd.Dir = rc.Dir
cmd.Stdout = rc.Stdout
cmd.Stderr = rc.Stderr
Expand Down
2 changes: 2 additions & 0 deletions modules/markup/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/process/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ func (pm *Manager) ExecDirEnvStdIn(timeout time.Duration, dir, desc string, env
if stdIn != nil {
cmd.Stdin = stdIn
}
SetSysProcAttribute(cmd)

if err := cmd.Start(); err != nil {
return "", "", err
}

err := cmd.Wait()

if err != nil {
err = &Error{
PID: GetPID(ctx),
Expand Down
18 changes: 18 additions & 0 deletions modules/process/manager_unix.go
Original file line number Diff line number Diff line change
@@ -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}
}
16 changes: 16 additions & 0 deletions modules/process/manager_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions modules/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func sessionHandler(session ssh.Session) {
}
defer stdin.Close()

process.SetSysProcAttribute(cmd)

wg := &sync.WaitGroup{}
wg.Add(2)

Expand Down
1 change: 1 addition & 0 deletions services/mailer/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func (s *sendmailSender) Send(from string, to []string, msg io.WriterTo) error {
if err != nil {
return err
}
process.SetSysProcAttribute(cmd)

if err = cmd.Start(); err != nil {
_ = pipe.Close()
Expand Down

0 comments on commit b3aa38b

Please sign in to comment.