Skip to content

Commit 3764db2

Browse files
dgannon991imjasonh
andauthored
Fix windows race condition when writing image with duplicate layers (#1921)
* Add mutex around os.Rename when on windows Signed-off-by: David Gannon <[email protected]> * Update pkg/v1/layout/write.go --------- Signed-off-by: David Gannon <[email protected]> Co-authored-by: Jason Hall <[email protected]>
1 parent 39d1148 commit 3764db2

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pkg/v1/layout/write.go

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"io"
2323
"os"
2424
"path/filepath"
25+
"runtime"
26+
"sync"
2527

2628
"github.com/google/go-containerregistry/pkg/logs"
2729
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -37,6 +39,9 @@ var layoutFile = `{
3739
"imageLayoutVersion": "1.0.0"
3840
}`
3941

42+
// renameMutex guards os.Rename calls in AppendImage on Windows only.
43+
var renameMutex sync.Mutex
44+
4045
// AppendImage writes a v1.Image to the Path and updates
4146
// the index.json to reference it.
4247
func (l Path) AppendImage(img v1.Image, options ...Option) error {
@@ -259,6 +264,11 @@ func (l Path) writeBlob(hash v1.Hash, size int64, rc io.ReadCloser, renamer func
259264
}
260265

261266
renamePath := l.path("blobs", finalHash.Algorithm, finalHash.Hex)
267+
268+
if runtime.GOOS == "windows" {
269+
renameMutex.Lock()
270+
defer renameMutex.Unlock()
271+
}
262272
return os.Rename(w.Name(), renamePath)
263273
}
264274

0 commit comments

Comments
 (0)