{"record":{"id":"30001f9442dbcbf1","repo":"hashicorp/nomad","slug":"could-not-stat-file-v","errorCode":null,"errorMessage":"could not stat file: %v","messagePattern":"could not stat file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/escapingfs/copydir.go","lineNumber":44,"sourceCode":"\t\tif d.IsDir() {\n\t\t\tinfo, err := d.Info()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"could not stat directory: %v\", err)\n\t\t\t}\n\t\t\treturn os.MkdirAll(newPath, info.Mode())\n\t\t}\n\t\tif !d.Type().IsRegular() {\n\t\t\treturn fmt.Errorf(\"copying cannot traverse symlinks\")\n\t\t}\n\n\t\tr, err := srcFs.Open(oldPath)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not open existing file: %v\", err)\n\t\t}\n\t\tdefer r.Close()\n\t\tinfo, err := r.Stat()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not stat file: %v\", err)\n\t\t}\n\n\t\tw, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode())\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif _, err := io.Copy(w, r); err != nil {\n\t\t\tw.Close()\n\t\t\treturn fmt.Errorf(\"could not copy file: %v\", err)\n\t\t}\n\t\treturn w.Close()\n\t})\n}\n","sourceCodeStart":26,"sourceCodeEnd":59,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/escapingfs/copydir.go#L26-L59","documentation":"CopyDir successfully opens a regular file but then calls r.Stat() on the open handle to learn its mode for the destination. If that Stat fails (unexpected filesystem/I-O failure on an already-open handle), the walk aborts with this wrapped error.","triggerScenarios":"CopyDir on a source file whose open file handle cannot be stat'ed — typically OS-level filesystem errors, network filesystems dropping the handle mid-operation, or a race where the file vanishes/changes state right after open.","commonSituations":"NFS/SMB/network mounts with unstable handles; container overlayfs edge cases; filesystem corruption; extremely rare since the file was just opened successfully.","solutions":["Retry the CopyDir; transient network-filesystem failures often resolve","Check filesystem health (`fsck`, dmesg, mount status) if the error persists","Avoid copying directly across unstable network mounts — copy locally first or remount the share","Verify no concurrent process is racing to remove/rename the file during the copy"],"exampleFix":"// before\n$ nomad ... # copy from an NFS mount that dropped\n// after\n$ cp -r /mnt/nfs/taskdir /tmp/staging  # copy locally first, verify, then CopyDir from staging","handlingStrategy":"retry","validationCode":"info, err := os.Stat(path)\nif err != nil { return fmt.Errorf(\"file %s cannot be statted: %w\", path, err) }","typeGuard":null,"tryCatchPattern":"if err := escapingfs.CopyDir(src, dst); err != nil {\n    if strings.HasPrefix(err.Error(), \"could not stat file\") {\n        time.Sleep(500 * time.Millisecond)\n        return escapingfs.CopyDir(src, dst) // transient FS failures often resolve\n    }\n    return err\n}","preventionTips":["Prefer stable local filesystems over network mounts for source directories","Retry transient I/O errors with backoff","Check filesystem/mount health if errors recur (dmesg, fsck, remount)","Avoid concurrent mutation of the source directory during the copy"],"tags":["filesystem","stat","io"],"backgroundTag":"file-stat-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}