Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce data races #14549

Merged
merged 7 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions modules/git/blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"io"
"strconv"
"strings"

gitea_log "code.gitea.io/gitea/modules/log"
)

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

go func() {
stderr := &strings.Builder{}
err = NewCommand("cat-file", "--batch").RunInDirFullPipeline(b.repoPath, stdoutWriter, stderr, strings.NewReader(b.ID.String()+"\n"))
err := NewCommand("cat-file", "--batch").RunInDirFullPipeline(b.repoPath, stdoutWriter, stderr, strings.NewReader(b.ID.String()+"\n"))
if err != nil {
err = ConcatenateError(err, stderr.String())
gitea_log.Error("Blob.DataAsync Error: %v", err)
_ = stdoutWriter.CloseWithError(err)
} else {
_ = stdoutWriter.Close()
Expand All @@ -50,8 +52,8 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
return &LimitedReaderCloser{
R: bufReader,
C: stdoutReader,
N: int64(size),
}, err
N: size,
}, nil
}

// Size returns the uncompressed size of the blob
Expand Down
5 changes: 3 additions & 2 deletions modules/process/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
// ErrExecTimeout represent a timeout error
ErrExecTimeout = errors.New("Process execution timeout")
manager *Manager
managerInit sync.Once

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

// GetManager returns a Manager and initializes one as singleton if there's none yet
func GetManager() *Manager {
if manager == nil {
managerInit.Do(func() {
lunny marked this conversation as resolved.
Show resolved Hide resolved
manager = &Manager{
processes: make(map[int64]*Process),
}
}
})
return manager
}

Expand Down
9 changes: 9 additions & 0 deletions modules/process/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetManager(t *testing.T) {
go func() {
// test race protection
_ = GetManager()
}()
pm := GetManager()
assert.NotNil(t, pm)
}

func TestManager_Add(t *testing.T) {
pm := Manager{processes: make(map[int64]*Process)}

Expand Down