Skip to content

Commit 04f65d2

Browse files
jarojasm95alexey-igrychev
authored andcommitted
fix(build/stages): Add RWMutex to ImagesTree
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>
1 parent 197e9cc commit 04f65d2

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pkg/build/image/image_tree.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"reflect"
99
"sort"
10+
"sync"
1011

1112
"github.com/werf/logboek"
1213
"github.com/werf/logboek/pkg/types"
@@ -27,6 +28,7 @@ type ImagesTree struct {
2728
imagesSets ImagesSets
2829

2930
multiplatformImages []*MultiplatformImage
31+
mutex sync.RWMutex
3032
}
3133

3234
type ImagesTreeOptions struct {
@@ -39,6 +41,7 @@ func NewImagesTree(werfConfig *config.WerfConfig, opts ImagesTreeOptions) *Image
3941
return &ImagesTree{
4042
ImagesTreeOptions: opts,
4143
werfConfig: werfConfig,
44+
mutex: sync.RWMutex{},
4245
}
4346
}
4447

@@ -189,6 +192,8 @@ func (tree *ImagesTree) GetImagesSets() ImagesSets {
189192
}
190193

191194
func (tree *ImagesTree) GetMultiplatformImage(name string) *MultiplatformImage {
195+
tree.mutex.RLock()
196+
defer tree.mutex.RUnlock()
192197
for _, img := range tree.multiplatformImages {
193198
if img.Name == name {
194199
return img
@@ -198,6 +203,8 @@ func (tree *ImagesTree) GetMultiplatformImage(name string) *MultiplatformImage {
198203
}
199204

200205
func (tree *ImagesTree) SetMultiplatformImage(newImg *MultiplatformImage) {
206+
tree.mutex.Lock()
207+
defer tree.mutex.Unlock()
201208
for _, img := range tree.multiplatformImages {
202209
if img.Name == newImg.Name {
203210
return
@@ -207,6 +214,8 @@ func (tree *ImagesTree) SetMultiplatformImage(newImg *MultiplatformImage) {
207214
}
208215

209216
func (tree *ImagesTree) GetMultiplatformImages() []*MultiplatformImage {
217+
tree.mutex.RLock()
218+
defer tree.mutex.RUnlock()
210219
return tree.multiplatformImages
211220
}
212221

0 commit comments

Comments
 (0)