Skip to content

Add retry for os.rename in atomicFileWriter #219

Description

@karman-docker

Problem: Updating files using atomicFileWriter can fail during rename on Windows, due to AV software holding the file handle.

Reproduction steps:
Run this simple program on Windows

package main

import (
	"fmt"
	"log"
	//"time"

	"github.com/moby/sys/atomicwriter"
)

func main() {
	data := []byte(`{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "experimental": false
}`)
	var count uint64
        fmt.Println("starting the test")
	for {
		count++
		//time.Sleep(50 * time.Millisecond)
		err := atomicwriter.WriteFile("test_daemon.json", data, 0o644)
		if err != nil {
			log.Fatalf("write failed after %d iterations: %v\n", count, err)
		}
	}
}

It will fail with below error after 1 or 2 minutes:

2026/04/24 12:16:24 write failed after 7807 iterations: rename C:\Users\azureuser\tmp\.tmp-test_daemon.json854562021 C:\Users\azureuser\tmp\test_daemon.json: Access is denied.
exit status 1

I just have Microsoft Defender on this system with default configuration.

Some environments (finance sector customers) have strict security policy where customers cannot exclude folders from AV scans.

Adding a retry around os.rename at https://github.com/moby/sys/blob/main/atomicwriter/atomicwriter.go#L155 can avoid this transient rename failure.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions