git: avoid leaking lock files on cancellation

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-19.03
Tonis Tiigi 2019-01-22 14:04:54 -08:00
parent 9c638c446a
commit e1c0729349
2 changed files with 12 additions and 1 deletions

View File

@ -248,6 +248,9 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context) (out cache.ImmutableRe
} }
if doFetch { if doFetch {
// make sure no old lock files have leaked
os.RemoveAll(filepath.Join(gitDir, "shallow.lock"))
args := []string{"fetch"} args := []string{"fetch"}
if !isCommitSHA(ref) { // TODO: find a branch from ls-remote? if !isCommitSHA(ref) { // TODO: find a branch from ls-remote?
args = append(args, "--depth=1", "--no-tags") args = append(args, "--depth=1", "--no-tags")

View File

@ -6,6 +6,7 @@ import (
"context" "context"
"os/exec" "os/exec"
"syscall" "syscall"
"time"
) )
func runProcessGroup(ctx context.Context, cmd *exec.Cmd) error { func runProcessGroup(ctx context.Context, cmd *exec.Cmd) error {
@ -17,7 +18,14 @@ func runProcessGroup(ctx context.Context, cmd *exec.Cmd) error {
go func() { go func() {
select { select {
case <-ctx.Done(): case <-ctx.Done():
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
go func() {
select {
case <-waitDone:
case <-time.After(10 * time.Second):
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}()
case <-waitDone: case <-waitDone:
} }
}() }()