Skip to content

Commit

Permalink
Fix windows race condition when writing image with duplicate layers (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
dgannon991 and imjasonh authored May 30, 2024
1 parent 39d1148 commit 3764db2
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/v1/layout/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"sync"

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

// renameMutex guards os.Rename calls in AppendImage on Windows only.
var renameMutex sync.Mutex

// AppendImage writes a v1.Image to the Path and updates
// the index.json to reference it.
func (l Path) AppendImage(img v1.Image, options ...Option) error {
Expand Down Expand Up @@ -259,6 +264,11 @@ func (l Path) writeBlob(hash v1.Hash, size int64, rc io.ReadCloser, renamer func
}

renamePath := l.path("blobs", finalHash.Algorithm, finalHash.Hex)

if runtime.GOOS == "windows" {
renameMutex.Lock()
defer renameMutex.Unlock()
}
return os.Rename(w.Name(), renamePath)
}

Expand Down

0 comments on commit 3764db2

Please sign in to comment.