Skip to content

Commit aee00b1

Browse files
authored
Fix race in mutate (#1627)
1 parent 6ac92e8 commit aee00b1

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

pkg/v1/mutate/image.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"encoding/json"
2020
"errors"
21+
"sync"
2122

2223
v1 "github.com/google/go-containerregistry/pkg/v1"
2324
"github.com/google/go-containerregistry/pkg/v1/partial"
@@ -38,6 +39,8 @@ type image struct {
3839
diffIDMap map[v1.Hash]v1.Layer
3940
digestMap map[v1.Hash]v1.Layer
4041
subject *v1.Descriptor
42+
43+
sync.Mutex
4144
}
4245

4346
var _ v1.Image = (*image)(nil)
@@ -50,6 +53,9 @@ func (i *image) MediaType() (types.MediaType, error) {
5053
}
5154

5255
func (i *image) compute() error {
56+
i.Lock()
57+
defer i.Unlock()
58+
5359
// Don't re-compute if already computed.
5460
if i.computed {
5561
return nil

pkg/v1/mutate/index.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package mutate
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"sync"
2021

2122
"github.com/google/go-containerregistry/pkg/logs"
2223
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -71,6 +72,8 @@ type index struct {
7172
indexMap map[v1.Hash]v1.ImageIndex
7273
layerMap map[v1.Hash]v1.Layer
7374
subject *v1.Descriptor
75+
76+
sync.Mutex
7477
}
7578

7679
var _ v1.ImageIndex = (*index)(nil)
@@ -85,6 +88,9 @@ func (i *index) MediaType() (types.MediaType, error) {
8588
func (i *index) Size() (int64, error) { return partial.Size(i) }
8689

8790
func (i *index) compute() error {
91+
i.Lock()
92+
defer i.Unlock()
93+
8894
// Don't re-compute if already computed.
8995
if i.computed {
9096
return nil

0 commit comments

Comments
 (0)