{"record":{"id":"529c7a610d53f18d","repo":"dagger/dagger","slug":"copy-file-range-failed","errorCode":null,"errorMessage":"copy file range failed","messagePattern":"copy file range failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/fsutil/copy/copy_linux.go","lineNumber":144,"sourceCode":"\t}\n\n\tvar written int64\n\tsize := st.Size()\n\tfirst := true\n\n\tfor written < size {\n\t\tvar desired int\n\t\tif size-written > math.MaxInt32 {\n\t\t\tdesired = int(math.MaxInt32)\n\t\t} else {\n\t\t\tdesired = int(size - written)\n\t\t}\n\n\t\tn, err := unix.CopyFileRange(int(src.Fd()), nil, int(dst.Fd()), nil, desired, 0)\n\t\tif err != nil {\n\t\t\t// matches go/src/internal/poll/copy_file_range_linux.go\n\t\t\tif (err != unix.ENOSYS && err != unix.EXDEV && err != unix.EPERM && err != syscall.EIO && err != unix.EOPNOTSUPP && err != syscall.EINVAL) || !first {\n\t\t\t\treturn errors.Wrap(err, \"copy file range failed\")\n\t\t\t}\n\n\t\t\tbuf := bufferPool.Get().(*[]byte)\n\t\t\t_, err = io.CopyBuffer(dst, src, *buf)\n\t\t\tbufferPool.Put(buf)\n\t\t\tif err != nil {\n\t\t\t\treturn errors.Wrap(err, \"userspace copy failed\")\n\t\t\t}\n\t\t\treturn nil\n\t\t}\n\n\t\tfirst = false\n\t\twritten += int64(n)\n\t}\n\treturn nil\n}\n\nfunc mknod(dst string, mode uint32, rDev int) error {","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/internal/fsutil/copy/copy_linux.go#L126-L162","documentation":"This error is wrapped in fsutil/copy.CopyFileContent when the Linux copy_file_range(2) syscall fails in a way the fast path cannot tolerate. The code retries with a userspace io.CopyBuffer only when the failure is a known-recoverable errno (ENOSYS, EXDEV, EPERM, EIO, EOPNOTSUPP, EINVAL) on the first chunk; any other errno, or a recoverable errno after the first chunk, is returned wrapped as 'copy file range failed'. It signals that the kernel-assisted copy path was aborted mid-file.","triggerScenarios":"Calling CopyFileContent (via copyFile during directory/copy operations) where unix.CopyFileRange returns an errno outside the tolerated set (e.g. EACCES, EBADF, ETXTBSY), or where a tolerated errno occurs when first==false (i.e. part of the file was already copied).","commonSituations":"Copying files across exotic mount points/devices where copy_file_range is partially supported (overlayfs, NFS, FUSE), source or destination file descriptors becoming invalid mid-copy, files modified/truncated concurrently during the copy, or running on kernel/container combinations with incomplete copy_file_range support.","solutions":["Check that both source and destination files exist, are regular files, and are not being modified concurrently during the copy.","Verify filesystem support: copy_file_range can fail on overlayfs/NFS/FUSE mounts; copy to a filesystem-backed temp location first, or fall back to a plain userspace copy.","Retry the whole copy operation from scratch if a partial copy was in progress (first==false case) since a recoverable errno after the first chunk is not retried in-process.","Update the kernel/container runtime if the errno indicates unsupported operation (ENOSYS) in containers with restricted syscalls; check seccomp profiles for copy_file_range allowlisting.","If you control the caller, pre-check permissions (EACCES/EPERM) on both files before starting the copy."],"exampleFix":"// before (caller, naive copy)\nerr := fsutil.CopyFileContent(src, dst)\n\n// after (caller, resilient)\nerr := fsutil.CopyFileContent(src, dst)\nif err != nil && strings.Contains(err.Error(), \"copy file range failed\") {\n    // fall back to a manual open+io.Copy\n    sf, serr := os.Open(src)\n    if serr != nil { return serr }\n    defer sf.Close()\n    df, derr := os.Create(dst)\n    if derr != nil { return derr }\n    defer df.Close()\n    _, err = io.Copy(df, sf)\n}","handlingStrategy":"fallback","validationCode":"func canCopyFileRange(src, dst *os.File) error {\n    si, err := src.Stat()\n    if err != nil || !si.Mode().IsRegular() {\n        return fmt.Errorf(\"src not a regular file: %w\", err)\n    }\n    di, err := dst.Stat()\n    if err != nil || !di.Mode().IsRegular() {\n        return fmt.Errorf(\"dst not a regular file: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"err := fsutil.CopyFileContent(srcF, dstF)\nif err != nil && strings.Contains(err.Error(), \"copy file range failed\") {\n    // reset file offsets and redo the copy in userspace\n    srcF.Seek(0, io.SeekStart); dstF.Seek(0, io.SeekStart)\n    _, err = io.Copy(dstF, srcF)\n}","preventionTips":["Pre-check that both src and dst are regular files on filesystems known to support copy_file_range (ext4/xfs/btrfs, same mount).","Copy to a temp file in the destination directory, then rename atomically, so partial-copy failures never corrupt the target.","Check the seccomp profile of your container allows copy_file_range if running in restricted environments.","Never mutate or truncate source files while a copy is in progress.","Keep a userspace io.Copy fallback wired in for any caller of CopyFileContent."],"tags":["linux","filesystem","copy-file-range","syscall"],"backgroundTag":"copy-file-range-failed","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}