Skip to content

Commit d5e466c

Browse files
committed
Make cancel from CatFileBatch and CatFileBatchCheck wait for the command to end
Fix go-gitea#16427 (again!) Signed-off-by: Andrew Thornton <[email protected]>
1 parent e6c2225 commit d5e466c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

modules/git/batch_reader.go

Lines changed: 9 additions & 3 deletions
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
}

0 commit comments

Comments
 (0)