Skip to content

Commit

Permalink
fix(build): fix data races (#6477)
Browse files Browse the repository at this point in the history
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com>
  • Loading branch information
iapershin authored Dec 11, 2024
1 parent fc41d2a commit 4e94905
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
9 changes: 7 additions & 2 deletions pkg/build/conveyor.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ func (c *Conveyor) GetImportServer(ctx context.Context, targetPlatform, imageNam
}

func (c *Conveyor) AppendOnTerminateFunc(f func() error) {
c.GetServiceRWMutex("TerminateFunctions").Lock()
defer c.GetServiceRWMutex("TerminateFunctions").Unlock()
c.onTerminateFuncs = append(c.onTerminateFuncs, f)
}

Expand Down Expand Up @@ -359,8 +361,8 @@ func (c *Conveyor) GetRemoteGitRepo(key string) *git_repo.Remote {
}

func (c *Conveyor) SetShouldAddManagedImagesRecords() {
c.GetServiceRWMutex("ShouldAddManagedImagesRecords").RLock()
defer c.GetServiceRWMutex("ShouldAddManagedImagesRecords").RUnlock()
c.GetServiceRWMutex("ShouldAddManagedImagesRecords").Lock()
defer c.GetServiceRWMutex("ShouldAddManagedImagesRecords").Unlock()
c.shouldAddManagedImagesRecords = true
}

Expand Down Expand Up @@ -637,6 +639,7 @@ func (c *Conveyor) doImagesInParallel(ctx context.Context, phases []Phase, logIm
numberOfWorkers := int(c.ParallelTasksLimit)

var setImageExecutionTimes []string
setImageExecutionTimesMutex := c.GetServiceRWMutex("SetImageExecutionTimes")
if err := parallel.DoTasks(ctx, numberOfTasks, parallel.DoTasksOptions{
InitDockerCLIForEachWorker: true,
MaxNumberOfWorkers: numberOfWorkers,
Expand All @@ -657,10 +660,12 @@ func (c *Conveyor) doImagesInParallel(ctx context.Context, phases []Phase, logIm

taskEndTime := time.Now()
taskDuration := taskEndTime.Sub(taskStartTime)
setImageExecutionTimesMutex.Lock()
setImageExecutionTimes = append(
setImageExecutionTimes,
fmt.Sprintf("%s (%.2f seconds)", taskImage.LogDetailedName(), taskDuration.Seconds()),
)
setImageExecutionTimesMutex.Unlock()
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/docker_registry/docker_registry_with_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (r *DockerRegistryWithCache) withCachedTags(reference string, f func([]stri
tags = value.([]string)
}

newTags, err := f(tags, isExist)
newTags, err := f(tags, isExist) // TODO(iapershin) fix data race here
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/true_git/git_cmd.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package true_git

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os/exec"

"github.com/werf/logboek"
"github.com/werf/werf/v2/pkg/util"
)

func NewGitCmd(ctx context.Context, opts *GitCmdOptions, cliArgs ...string) GitCmd {
Expand All @@ -21,9 +21,9 @@ func NewGitCmd(ctx context.Context, opts *GitCmdOptions, cliArgs ...string) GitC
}

gitCmd := GitCmd{
OutBuf: &bytes.Buffer{},
ErrBuf: &bytes.Buffer{},
OutErrBuf: &bytes.Buffer{},
OutBuf: util.NewGoroutineSafeBuffer(),
ErrBuf: util.NewGoroutineSafeBuffer(),
OutErrBuf: util.NewGoroutineSafeBuffer(),
}

gitCmd.Cmd = exec.Command("git", append(getCommonGitOptions(), cliArgs...)...)
Expand Down Expand Up @@ -52,9 +52,9 @@ type GitCmd struct {
*exec.Cmd

// We always write to all of these buffs, unlike with exec.Cmd.Stdout(Stderr)
OutBuf *bytes.Buffer
ErrBuf *bytes.Buffer
OutErrBuf *bytes.Buffer
OutBuf *util.GoroutineSafeBuffer
ErrBuf *util.GoroutineSafeBuffer
OutErrBuf *util.GoroutineSafeBuffer
}

func (c *GitCmd) Run(ctx context.Context) error {
Expand Down
4 changes: 3 additions & 1 deletion pkg/true_git/service_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/Masterminds/semver"

"github.com/werf/werf/v2/pkg/util"
)

type SyncSourceWorktreeWithServiceBranchOptions struct {
Expand Down Expand Up @@ -175,7 +177,7 @@ func addNewChangesInServiceWorktreeDir(ctx context.Context, sourceWorktreeDir, s
return err
}

func runGitAddCmd(ctx context.Context, sourceWorktreeDir, serviceWorktreeDir string, globExcludeList []string, dryRun bool) (*bytes.Buffer, error) {
func runGitAddCmd(ctx context.Context, sourceWorktreeDir, serviceWorktreeDir string, globExcludeList []string, dryRun bool) (*util.GoroutineSafeBuffer, error) {
gitAddArgs := []string{
"--work-tree",
sourceWorktreeDir,
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/goroutine_safe_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type GoroutineSafeBuffer struct {
m sync.Mutex
}

func NewGoroutineSafeBuffer() *GoroutineSafeBuffer {
return &GoroutineSafeBuffer{Buffer: bytes.NewBuffer([]byte{})}
}

func (b *GoroutineSafeBuffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
Expand Down

0 comments on commit 4e94905

Please sign in to comment.