Skip to content

Commit 3299f04

Browse files
authored
Make cancel from CatFileBatch and CatFileBatchCheck wait for the command to end (#16479) (#16481)
* Make cancel from CatFileBatch and CatFileBatchCheck wait for the command to end Fix #16427 (again!) * handle sharing violation error code Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent e6c2225 commit 3299f04

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

modules/git/batch_reader.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package git
77
import (
88
"bufio"
99
"bytes"
10+
"context"
1011
"io"
1112
"math"
1213
"strconv"
@@ -15,31 +16,36 @@ import (
1516

1617
// CatFileBatch opens git cat-file --batch in the provided repo and returns a stdin pipe, a stdout reader and cancel function
1718
func CatFileBatch(repoPath string) (*io.PipeWriter, *bufio.Reader, func()) {
18-
// Next feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
19+
// We often want to feed the commits in order into cat-file --batch, followed by their trees and sub trees as necessary.
1920
// so let's create a batch stdin and stdout
2021
batchStdinReader, batchStdinWriter := io.Pipe()
2122
batchStdoutReader, batchStdoutWriter := io.Pipe()
23+
ctx, ctxCancel := context.WithCancel(DefaultContext)
24+
closed := make(chan struct{})
2225
cancel := func() {
2326
_ = batchStdinReader.Close()
2427
_ = batchStdinWriter.Close()
2528
_ = batchStdoutReader.Close()
2629
_ = batchStdoutWriter.Close()
30+
ctxCancel()
31+
<-closed
2732
}
2833

2934
go func() {
3035
stderr := strings.Builder{}
31-
err := NewCommand("cat-file", "--batch").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
36+
err := NewCommandContext(ctx, "cat-file", "--batch").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
3237
if err != nil {
3338
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
3439
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
3540
} else {
3641
_ = batchStdoutWriter.Close()
3742
_ = batchStdinReader.Close()
3843
}
44+
close(closed)
3945
}()
4046

4147
// For simplicities sake we'll us a buffered reader to read from the cat-file --batch
42-
batchReader := bufio.NewReader(batchStdoutReader)
48+
batchReader := bufio.NewReaderSize(batchStdoutReader, 32*1024)
4349

4450
return batchStdinWriter, batchReader, cancel
4551
}

modules/util/remove.go

+21
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ package util
66

77
import (
88
"os"
9+
"runtime"
910
"syscall"
1011
"time"
1112
)
1213

14+
const windowsSharingViolationError syscall.Errno = 32
15+
1316
// Remove removes the named file or (empty) directory with at most 5 attempts.
1417
func Remove(name string) error {
1518
var err error
@@ -25,6 +28,12 @@ func Remove(name string) error {
2528
continue
2629
}
2730

31+
if unwrapped == windowsSharingViolationError && runtime.GOOS == "windows" {
32+
// try again
33+
<-time.After(100 * time.Millisecond)
34+
continue
35+
}
36+
2837
if unwrapped == syscall.ENOENT {
2938
// it's already gone
3039
return nil
@@ -48,6 +57,12 @@ func RemoveAll(name string) error {
4857
continue
4958
}
5059

60+
if unwrapped == windowsSharingViolationError && runtime.GOOS == "windows" {
61+
// try again
62+
<-time.After(100 * time.Millisecond)
63+
continue
64+
}
65+
5166
if unwrapped == syscall.ENOENT {
5267
// it's already gone
5368
return nil
@@ -71,6 +86,12 @@ func Rename(oldpath, newpath string) error {
7186
continue
7287
}
7388

89+
if unwrapped == windowsSharingViolationError && runtime.GOOS == "windows" {
90+
// try again
91+
<-time.After(100 * time.Millisecond)
92+
continue
93+
}
94+
7495
if i == 0 && os.IsNotExist(err) {
7596
return err
7697
}

0 commit comments

Comments
 (0)