Skip to content

Commit 195f50b

Browse files
authored
fix: close file handle before moving file in FullBlobUpload (#3499)
Should fix a Windows specific issue where renaming a file fails if the handler is not closed. Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
1 parent f00d386 commit 195f50b

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

pkg/storage/imagestore/imagestore.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,21 +1082,35 @@ func (is *ImageStore) FullBlobUpload(repo string, body io.Reader, dstDigest godi
10821082
return "", -1, zerr.ErrUploadNotFound
10831083
}
10841084

1085-
defer blobFile.Close()
1086-
10871085
mw := io.MultiWriter(blobFile, digester)
10881086

10891087
nbytes, err := io.Copy(mw, body)
10901088
if err != nil {
1089+
_ = blobFile.Close()
1090+
1091+
is.log.Error().Err(err).Str("blob", src).Msg("failed to write blob")
1092+
10911093
return "", -1, err
10921094
}
10931095

10941096
if err := blobFile.Commit(context.Background()); err != nil {
1097+
_ = blobFile.Close()
1098+
10951099
is.log.Error().Err(err).Str("blob", src).Msg("failed to commit blob")
10961100

10971101
return "", -1, err
10981102
}
10991103

1104+
// Close explicitly before returning so the subsequent move/rename can succeed on Windows.
1105+
// - Windows does not allow renaming/moving a file while there is any open handle to it.
1106+
// - If we relied on a deferred close, the handle would be released only when the function returns,
1107+
// which would prevent the move/rename operation from succeeding on Windows.
1108+
if err := blobFile.Close(); err != nil {
1109+
is.log.Error().Err(err).Msg("failed to close blob")
1110+
1111+
return "", -1, err
1112+
}
1113+
11001114
srcDigest := godigest.NewDigestFromEncoded(dstDigestAlgorithm, hex.EncodeToString(digester.Sum(nil)))
11011115
if srcDigest != dstDigest {
11021116
is.log.Error().Str("srcDigest", srcDigest.String()).

0 commit comments

Comments
 (0)