From 23826a350f3e6dfa26afd8433ce948ec5105ac39 Mon Sep 17 00:00:00 2001 From: Yuichi Watanabe Date: Mon, 17 Sep 2018 07:46:45 +0900 Subject: [PATCH 1/2] clean up command process (#5) --- githttpxfer/git.go | 2 ++ githttpxfer/githttpxfer.go | 6 ++++++ githttpxfer/support.go | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/githttpxfer/git.go b/githttpxfer/git.go index ab3461a..de667f9 100644 --- a/githttpxfer/git.go +++ b/githttpxfer/git.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path" + "syscall" ) func newGit(rootPath string, binPath string, uploadPack bool, receivePack bool) *git { @@ -49,6 +50,7 @@ func (g *git) Exists(repoPath string) bool { func (g *git) GitCommand(repoPath string, args ...string) *exec.Cmd { command := exec.Command(g.binPath, args...) command.Dir = g.GetAbsolutePath(repoPath) + command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} return command } diff --git a/githttpxfer/githttpxfer.go b/githttpxfer/githttpxfer.go index b031b7b..c93deae 100644 --- a/githttpxfer/githttpxfer.go +++ b/githttpxfer/githttpxfer.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "os/exec" "regexp" "strings" "sync" @@ -266,6 +267,11 @@ func (ghx *GitHTTPXfer) serviceRPC(ctx Context, rpc string) { args := []string{rpc, "--stateless-rpc", "."} cmd := ghx.Git.GitCommand(repoPath, args...) cmd.Env = ctx.Env() + defer func(cmd *exec.Cmd) { + if err := cleanUpProcessGroup(cmd); err != nil { + ghx.logger.Error("clean up process group -> ", err.Error()) + } + }(cmd) stdin, err := cmd.StdinPipe() if err != nil { diff --git a/githttpxfer/support.go b/githttpxfer/support.go index e987b74..dc917d4 100644 --- a/githttpxfer/support.go +++ b/githttpxfer/support.go @@ -2,7 +2,12 @@ package githttpxfer import ( "net/http" + "os/exec" "strings" + "syscall" + + "errors" + "fmt" ) func getServiceType(req *http.Request) string { @@ -12,3 +17,18 @@ func getServiceType(req *http.Request) string { } return strings.Replace(serviceType, "git-", "", 1) } + +func cleanUpProcessGroup(cmd *exec.Cmd) error { + if cmd == nil { + return errors.New("cmd is nil") + } + var err error + process := cmd.Process + if process != nil && process.Pid > 0 { + if e := syscall.Kill(-process.Pid, syscall.SIGTERM); e != nil { + err = fmt.Errorf(e.Error()+" [pgid %d]", -process.Pid) + } + } + cmd.Wait() + return err +} From 1be2b3662b811e25eb548c98f8e55a54fd42671d Mon Sep 17 00:00:00 2001 From: Yuichi Watanabe Date: Thu, 15 Nov 2018 11:36:37 +0900 Subject: [PATCH 2/2] remove log from clean up function (#5) --- githttpxfer/githttpxfer.go | 10 +++------- githttpxfer/support.go | 16 ++++------------ 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/githttpxfer/githttpxfer.go b/githttpxfer/githttpxfer.go index c93deae..6572715 100644 --- a/githttpxfer/githttpxfer.go +++ b/githttpxfer/githttpxfer.go @@ -7,7 +7,6 @@ import ( "net/http" "net/url" "os" - "os/exec" "regexp" "strings" "sync" @@ -267,11 +266,7 @@ func (ghx *GitHTTPXfer) serviceRPC(ctx Context, rpc string) { args := []string{rpc, "--stateless-rpc", "."} cmd := ghx.Git.GitCommand(repoPath, args...) cmd.Env = ctx.Env() - defer func(cmd *exec.Cmd) { - if err := cleanUpProcessGroup(cmd); err != nil { - ghx.logger.Error("clean up process group -> ", err.Error()) - } - }(cmd) + defer cleanUpProcessGroup(cmd) stdin, err := cmd.StdinPipe() if err != nil { @@ -289,7 +284,8 @@ func (ghx *GitHTTPXfer) serviceRPC(ctx Context, rpc string) { } defer stdout.Close() - if err = cmd.Start(); err != nil { + err = cmd.Start() + if err != nil { ghx.logger.Error("failed to starts the specified command. ", err.Error()) RenderInternalServerError(res.Writer) return diff --git a/githttpxfer/support.go b/githttpxfer/support.go index dc917d4..28fd678 100644 --- a/githttpxfer/support.go +++ b/githttpxfer/support.go @@ -5,9 +5,6 @@ import ( "os/exec" "strings" "syscall" - - "errors" - "fmt" ) func getServiceType(req *http.Request) string { @@ -18,17 +15,12 @@ func getServiceType(req *http.Request) string { return strings.Replace(serviceType, "git-", "", 1) } -func cleanUpProcessGroup(cmd *exec.Cmd) error { +func cleanUpProcessGroup(cmd *exec.Cmd) { if cmd == nil { - return errors.New("cmd is nil") + return } - var err error - process := cmd.Process - if process != nil && process.Pid > 0 { - if e := syscall.Kill(-process.Pid, syscall.SIGTERM); e != nil { - err = fmt.Errorf(e.Error()+" [pgid %d]", -process.Pid) - } + if process := cmd.Process; process != nil && process.Pid > 0 { + syscall.Kill(-process.Pid, syscall.SIGTERM) } cmd.Wait() - return err }