Files
containerd/plugins/content/local/store_windows.go
T
ayush-panta a26143af1e content: handle sharing violations on Windows
On Windows, os.RemoveAll and os.ReadFile can fail with a sharing
violation when a file handle has not been fully released by the OS.
Unlike Unix, Windows does not allow deleting or reading a file
while any handle is open, and handles may be held briefly after
Close returns. This causes a race between the gRPC server-side
writer Close (which writes updatedat.tmp) and both Abort (which
calls RemoveAll on the ingest directory) and status (which reads
the updatedat file), surfacing as a flaky
TestContentClient/CommitErrorState on Windows CI.

Add platform-specific removePath and readFileWithRetry helpers
that retry with incremental backoff on Windows. On non-Windows
platforms they pass through to os.RemoveAll and os.ReadFile
directly. This follows the same pattern as ensureRemoveAll in
the CRI server helpers, which retries on EBUSY for mount races
on Linux.

Signed-off-by: ayush-panta <ayushkp@amazon.com>
2026-06-01 08:08:14 -07:00

98 lines
2.8 KiB
Go

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package local
import (
"errors"
"os"
"time"
"golang.org/x/sys/windows"
)
func getATime(fi os.FileInfo) time.Time {
return fi.ModTime()
}
const (
// maxFileRetries is the number of attempts for file operations that
// may fail due to transient sharing violations on Windows.
maxFileRetries = 5
// fileRetryDelay is the base delay between retries. Each attempt
// waits (attempt number) * fileRetryDelay.
fileRetryDelay = 100 * time.Millisecond
)
// isRetryableError returns true for Windows errors that are known to be
// transient when a file handle has not been fully released by the OS.
func isRetryableError(err error) bool {
return errors.Is(err, windows.ERROR_SHARING_VIOLATION) ||
errors.Is(err, windows.ERROR_LOCK_VIOLATION) ||
errors.Is(err, windows.ERROR_DIR_NOT_EMPTY)
}
// removePath wraps os.RemoveAll with a retry loop for Windows.
//
// On Windows, a file that has been closed by the application may
// still be held briefly by the OS. Unlike Unix, Windows does not
// allow deleting a file while any handle is open, so os.RemoveAll
// can fail with a sharing violation if it races with a recent Close.
// Retrying after a short delay allows the OS to finish releasing the handle.
func removePath(path string) error {
var err error
for i := range maxFileRetries {
err = os.RemoveAll(path)
if err == nil {
return nil
}
if !isRetryableError(err) {
return err
}
if i < maxFileRetries-1 {
time.Sleep(time.Duration(i+1) * fileRetryDelay)
}
}
return err
}
// readFileWithRetry wraps os.ReadFile with a retry loop for Windows.
//
// On Windows, a file that has been closed by the application may
// still be held briefly by the OS. Unlike Unix, Windows does not
// allow reading a file while an exclusive handle is open, so
// os.ReadFile can fail with a sharing violation if it races with
// a recent Close. Retrying after a short delay allows the OS to
// finish releasing the handle.
func readFileWithRetry(path string) ([]byte, error) {
var err error
for i := range maxFileRetries {
var b []byte
b, err = os.ReadFile(path)
if err == nil {
return b, nil
}
if os.IsNotExist(err) || !isRetryableError(err) {
return nil, err
}
if i < maxFileRetries-1 {
time.Sleep(time.Duration(i+1) * fileRetryDelay)
}
}
return nil, err
}