Skip to content

Commit

Permalink
Merge pull request #39 from nulab/issue-5/clean-up-command-process
Browse files Browse the repository at this point in the history
Issue 5/clean up command process
  • Loading branch information
vvatanabe committed Sep 16, 2018
2 parents 8c1746f + 709da12 commit 4170f89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions githttpxfer/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path"
"syscall"
)

func newGit(rootPath string, binPath string, uploadPack bool, receivePack bool) *git {
Expand Down Expand Up @@ -59,6 +60,7 @@ func (g *git) UpdateServerInfo(repoPath string) ([]byte, error) {
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
}

Expand Down
6 changes: 6 additions & 0 deletions githttpxfer/githttpxfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
"sync"
Expand Down Expand Up @@ -265,6 +266,11 @@ func (ghx *GitHTTPXfer) serviceRPC(ctx Context, rpc string) {

args := []string{rpc, "--stateless-rpc", "."}
cmd := ghx.Git.GitCommand(repoPath, args...)
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 {
Expand Down
20 changes: 20 additions & 0 deletions githttpxfer/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package githttpxfer

import (
"net/http"
"os/exec"
"strings"
"syscall"

"errors"
"fmt"
)

func getServiceType(req *http.Request) string {
Expand All @@ -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
}

0 comments on commit 4170f89

Please sign in to comment.