Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Add timeout to git command
Browse files Browse the repository at this point in the history
To mitigate against any low level execution problems, add a context timeout to git commands so that if they fail, it won't cause subsequent requests to back up.

Fixes #714
  • Loading branch information
squaremo authored and philwinder committed Aug 24, 2017
1 parent 23c72d1 commit 73947c4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import (
"path/filepath"
"strings"

"context"
"github.com/pkg/errors"
"github.com/weaveworks/flux/ssh"
"time"
)

const (
commandTimeout = 10 * time.Second
)

func config(workingDir, user, email string) error {
Expand Down Expand Up @@ -191,8 +197,10 @@ func changedFiles(path, subPath, ref string) ([]string, error) {
}

func execGitCmd(dir string, keyRing ssh.KeyRing, out io.Writer, args ...string) error {
// println("git", strings.Join(args, " "))
c := exec.Command("git", args...)
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, commandTimeout)
defer cancel()
c := exec.CommandContext(ctx, "git", args...)
if dir != "" {
c.Dir = dir
}
Expand All @@ -210,6 +218,9 @@ func execGitCmd(dir string, keyRing ssh.KeyRing, out io.Writer, args ...string)
err = errors.New(msg)
}
}
if ctx.Err() == context.DeadlineExceeded {
return errors.Wrap(ctx.Err(), fmt.Sprintf("running git command took longer than %s: %s %v", commandTimeout.String(), "git", args))
}
return err
}

Expand Down

0 comments on commit 73947c4

Please sign in to comment.