Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows race condition when writing image with duplicate layers #1921

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
imjasonh marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading