Skip to content

Commit 87009ab

Browse files
authored
Reduce data races (#14549)
* Add race conditions into test * Fix Race in GetManager() * DataAsync() use error chan * just log no chan * finish
1 parent 0d14447 commit 87009ab

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

modules/git/blob_nogogit.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"io"
1212
"strconv"
1313
"strings"
14+
15+
gitea_log "code.gitea.io/gitea/modules/log"
1416
)
1517

1618
// Blob represents a Git object.
@@ -27,13 +29,13 @@ type Blob struct {
2729
// Calling the Close function on the result will discard all unread output.
2830
func (b *Blob) DataAsync() (io.ReadCloser, error) {
2931
stdoutReader, stdoutWriter := io.Pipe()
30-
var err error
3132

3233
go func() {
3334
stderr := &strings.Builder{}
34-
err = NewCommand("cat-file", "--batch").RunInDirFullPipeline(b.repoPath, stdoutWriter, stderr, strings.NewReader(b.ID.String()+"\n"))
35+
err := NewCommand("cat-file", "--batch").RunInDirFullPipeline(b.repoPath, stdoutWriter, stderr, strings.NewReader(b.ID.String()+"\n"))
3536
if err != nil {
3637
err = ConcatenateError(err, stderr.String())
38+
gitea_log.Error("Blob.DataAsync Error: %v", err)
3739
_ = stdoutWriter.CloseWithError(err)
3840
} else {
3941
_ = stdoutWriter.Close()
@@ -50,8 +52,8 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
5052
return &LimitedReaderCloser{
5153
R: bufReader,
5254
C: stdoutReader,
53-
N: int64(size),
54-
}, err
55+
N: size,
56+
}, nil
5557
}
5658

5759
// Size returns the uncompressed size of the blob

modules/process/manager.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
// ErrExecTimeout represent a timeout error
2626
ErrExecTimeout = errors.New("Process execution timeout")
2727
manager *Manager
28+
managerInit sync.Once
2829

2930
// DefaultContext is the default context to run processing commands in
3031
DefaultContext = context.Background()
@@ -48,11 +49,11 @@ type Manager struct {
4849

4950
// GetManager returns a Manager and initializes one as singleton if there's none yet
5051
func GetManager() *Manager {
51-
if manager == nil {
52+
managerInit.Do(func() {
5253
manager = &Manager{
5354
processes: make(map[int64]*Process),
5455
}
55-
}
56+
})
5657
return manager
5758
}
5859

modules/process/manager_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
func TestGetManager(t *testing.T) {
16+
go func() {
17+
// test race protection
18+
_ = GetManager()
19+
}()
20+
pm := GetManager()
21+
assert.NotNil(t, pm)
22+
}
23+
1524
func TestManager_Add(t *testing.T) {
1625
pm := Manager{processes: make(map[int64]*Process)}
1726

0 commit comments

Comments
 (0)