hashicorp/nomad · error

could not stat file: %v

Error message

could not stat file: %v

What it means

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.

Source

Thrown at helper/escapingfs/copydir.go:44

		if d.IsDir() {
			info, err := d.Info()
			if err != nil {
				return fmt.Errorf("could not stat directory: %v", err)
			}
			return os.MkdirAll(newPath, info.Mode())
		}
		if !d.Type().IsRegular() {
			return fmt.Errorf("copying cannot traverse symlinks")
		}

		r, err := srcFs.Open(oldPath)
		if err != nil {
			return fmt.Errorf("could not open existing file: %v", err)
		}
		defer r.Close()
		info, err := r.Stat()
		if err != nil {
			return fmt.Errorf("could not stat file: %v", err)
		}

		w, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode())
		if err != nil {
			return err
		}

		if _, err := io.Copy(w, r); err != nil {
			w.Close()
			return fmt.Errorf("could not copy file: %v", err)
		}
		return w.Close()
	})
}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Retry the CopyDir; transient network-filesystem failures often resolve
  2. Check filesystem health (`fsck`, dmesg, mount status) if the error persists
  3. Avoid copying directly across unstable network mounts — copy locally first or remount the share
  4. Verify no concurrent process is racing to remove/rename the file during the copy

Example fix

// before
$ nomad ... # copy from an NFS mount that dropped
// after
$ cp -r /mnt/nfs/taskdir /tmp/staging  # copy locally first, verify, then CopyDir from staging
Defensive patterns

Strategy: retry

Validate before calling

info, err := os.Stat(path)
if err != nil { return fmt.Errorf("file %s cannot be statted: %w", path, err) }

Try / catch

if err := escapingfs.CopyDir(src, dst); err != nil {
    if strings.HasPrefix(err.Error(), "could not stat file") {
        time.Sleep(500 * time.Millisecond)
        return escapingfs.CopyDir(src, dst) // transient FS failures often resolve
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: NFS/SMB/network mounts with unstable handles; container overlayfs edge cases; filesystem corruption; extremely rare since the file was just opened successfully.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/30001f9442dbcbf1. Report an issue: GitHub.