golang/go · error

CreateFileMapping %s: %w

Error message

CreateFileMapping %s: %w

What it means

Windows mmap helper: `syscall.CreateFileMapping` failed when creating the backing mapping object. The wrapped error is the Windows syscall result — common causes are sharing violations (file open/locked elsewhere), insufficient memory, or path/access issues.

Source

Thrown at src/cmd/go/internal/mmap/mmap_windows.go:27

	"os"
	"syscall"
	"unsafe"

	"internal/syscall/windows"
)

func mmapFile(f *os.File) (Data, error) {
	st, err := f.Stat()
	if err != nil {
		return Data{}, err
	}
	size := st.Size()
	if size == 0 {
		return Data{f, nil}, nil
	}
	h, err := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, 0, 0, nil)
	if err != nil {
		return Data{}, fmt.Errorf("CreateFileMapping %s: %w", f.Name(), err)
	}

	addr, err := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, 0)
	if err != nil {
		return Data{}, fmt.Errorf("MapViewOfFile %s: %w", f.Name(), err)
	}
	var info windows.MemoryBasicInformation
	err = windows.VirtualQuery(addr, &info, unsafe.Sizeof(info))
	if err != nil {
		return Data{}, fmt.Errorf("VirtualQuery %s: %w", f.Name(), err)
	}
	data := unsafe.Slice((*byte)(unsafe.Pointer(addr)), int(info.RegionSize))
	if len(data) < int(size) {
		// In some cases, especially on 386, we may not receive a in incomplete mapping:
		// one that is shorter than the file itself. Return an error in those cases because
		// incomplete mappings are not useful.
		return Data{}, fmt.Errorf("mmapFile: received incomplete mapping of file")
	}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Close other processes that may hold the file (other `go` invocations, editors, AV).
  2. Run `go clean -modcache` to remove the contested file.
  3. Exclude the module cache directory from antivirus on-access scanning.
  4. Ensure sufficient memory/swap and avoid concurrent builds competing for the same cache.
Defensive patterns

Strategy: retry

Try / catch

// Retry Windows file-mapping failures that look like sharing violations.
for i := 0; i < 3; i++ {
    err := runGoCmd()
    if err == nil || !isSharingViolation(err) { return err }
    time.Sleep(time.Duration(1<<i) * time.Second)
}

Prevention

When it happens

Trigger: The file is locked by another process (antivirus, editor, concurrent `go` process); system commit limit reached; file on a restricted network share.

Common situations: Antivirus scanning locks cache files; multiple concurrent builds; low virtual memory; SMB share permission limits.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/07ef5b7cdfd12e90. Report an issue: GitHub.