Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Setpgid on child git processes #19865

Merged
merged 11 commits into from
Jun 3, 2022
2 changes: 2 additions & 0 deletions 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"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/lfs"
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 @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions modules/git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions modules/process/manager_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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}
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -102,6 +102,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 @@ -281,6 +281,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