diff options
author | Mura Li <typeless@users.noreply.github.com> | 2017-04-08 10:23:39 +0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-04-07 19:23:39 -0700 |
commit | edbb9eefd6c12d6c67bf864539a51f5a09c7d670 (patch) | |
tree | e1370d07f813fbe9fa75ff7675c917356e06475f /vendor/code.gitea.io/git/command.go | |
parent | 5c0bee9b20f50a26c7ac3fa90d1db2b8329d358b (diff) | |
download | gitea-edbb9eefd6c12d6c67bf864539a51f5a09c7d670.tar.gz gitea-edbb9eefd6c12d6c67bf864539a51f5a09c7d670.zip |
Fix race when running commands with timeout (#1465)
Update vendored module code.gitea.io/git
Diffstat (limited to 'vendor/code.gitea.io/git/command.go')
-rw-r--r-- | vendor/code.gitea.io/git/command.go | 27 |
1 files changed, 6 insertions, 21 deletions
diff --git a/vendor/code.gitea.io/git/command.go b/vendor/code.gitea.io/git/command.go index 43e6eea900..c3534a1456 100644 --- a/vendor/code.gitea.io/git/command.go +++ b/vendor/code.gitea.io/git/command.go @@ -6,6 +6,7 @@ package git import ( "bytes" + "context" "fmt" "io" "os/exec" @@ -58,7 +59,10 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std log("%s: %v", dir, c) } - cmd := exec.Command(c.name, c.args...) + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + cmd := exec.CommandContext(ctx, c.name, c.args...) cmd.Dir = dir cmd.Stdout = stdout cmd.Stderr = stderr @@ -66,26 +70,7 @@ func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, std return err } - done := make(chan error) - go func() { - done <- cmd.Wait() - }() - - var err error - select { - case <-time.After(timeout): - if cmd.Process != nil && cmd.ProcessState != nil && !cmd.ProcessState.Exited() { - if err := cmd.Process.Kill(); err != nil { - return fmt.Errorf("fail to kill process: %v", err) - } - } - - <-done - return ErrExecTimeout{timeout} - case err = <-done: - } - - return err + return cmd.Wait() } // RunInDirTimeout executes the command in given directory with given timeout, |