Skip to content

Commit

Permalink
fix(build/stages): Add RWMutex to ImagesTree
Browse files Browse the repository at this point in the history
Add RWMutex to ImagesTree to synchronize concurrent access of the "multiplatformImages" array. This is done to prevent race conditions where an image is not stored in the array because it is overwritten by a parallel task.

Signed-off-by: Jose Rojas <39174181+jarojasm95@users.noreply.github.com>
  • Loading branch information
jarojasm95 authored and alexey-igrychev committed Aug 21, 2024
1 parent ba781f7 commit 29855a9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/build/image/image_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"reflect"
"sort"
"sync"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/types"
Expand All @@ -27,6 +28,7 @@ type ImagesTree struct {
imagesSets ImagesSets

multiplatformImages []*MultiplatformImage
mutex sync.RWMutex
}

type ImagesTreeOptions struct {
Expand All @@ -40,6 +42,7 @@ func NewImagesTree(werfConfig *config.WerfConfig, opts ImagesTreeOptions) *Image
return &ImagesTree{
ImagesTreeOptions: opts,
werfConfig: werfConfig,
mutex: sync.RWMutex{},
}
}

Expand Down Expand Up @@ -194,6 +197,8 @@ func (tree *ImagesTree) GetImagesSets() ImagesSets {
}

func (tree *ImagesTree) GetMultiplatformImage(name string) *MultiplatformImage {
tree.mutex.RLock()
defer tree.mutex.RUnlock()
for _, img := range tree.multiplatformImages {
if img.Name == name {
return img
Expand All @@ -203,6 +208,8 @@ func (tree *ImagesTree) GetMultiplatformImage(name string) *MultiplatformImage {
}

func (tree *ImagesTree) SetMultiplatformImage(newImg *MultiplatformImage) {
tree.mutex.Lock()
defer tree.mutex.Unlock()
for _, img := range tree.multiplatformImages {
if img.Name == newImg.Name {
return
Expand All @@ -212,6 +219,8 @@ func (tree *ImagesTree) SetMultiplatformImage(newImg *MultiplatformImage) {
}

func (tree *ImagesTree) GetMultiplatformImages() []*MultiplatformImage {
tree.mutex.RLock()
defer tree.mutex.RUnlock()
return tree.multiplatformImages
}

Expand Down

0 comments on commit 29855a9

Please sign in to comment.