larksuite/cli · error

cannot stat opened file: %w

Error message

cannot stat opened file: %w

What it means

inspectOpenedFile could not f.Stat() the freshly opened descriptor. The library stats the fd after open to verify it matches the pre-open validation, so if that fstat fails it cannot confirm the file is safe and refuses it. This is an OS-level fstat failure on an already-open fd, so it is rare.

Source

Thrown at internal/vfs/localfileio/openvalidated_unix.go:61

	if err != nil {
		return nil, err
	}
	if err := inspectOpenedFile(f, pre, true); err != nil {
		f.Close()
		// An unusable target is a bad argument, not an internal fault: callers
		// map ErrPathValidation to a typed validation error, and the fd checks
		// are the same verdict the path checks make, one layer later.
		return nil, &fileio.PathValidationError{Err: err}
	}
	return f, nil
}

// inspectOpenedFile validates the opened fd and restores blocking mode. pre is
// nil when there is no prior Stat to compare against.
func inspectOpenedFile(f *os.File, pre os.FileInfo, rejectHardLinks bool) error {
	post, err := f.Stat()
	if err != nil {
		return fmt.Errorf("cannot stat opened file: %w", err)
	}
	if pre != nil && !os.SameFile(pre, post) {
		return fmt.Errorf("file changed between validation and open")
	}
	if !post.Mode().IsRegular() {
		return fmt.Errorf("not a regular file (directories, devices, FIFOs, and sockets are refused)")
	}
	if rejectHardLinks {
		if st, ok := post.Sys().(*syscall.Stat_t); ok && st.Nlink > 1 {
			return fmt.Errorf("file has multiple hard links, so the other names it can be reached by " +
				"cannot be checked (hint: copy the file and use the copy instead)")
		}
	}
	if err := syscall.SetNonblock(int(f.Fd()), false); err != nil {
		return fmt.Errorf("cannot restore blocking mode: %w", err)
	}
	return nil
}

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Check for concurrent Close of the *os.File in your code between OpenFile and use
  2. Raise the file-descriptor limit (ulimit -n) if EMFILE/ENFILE appears
  3. Check the wrapped errno (%w cause) for EIO — verify the underlying filesystem mount is healthy
  4. Retry the operation; a transient fstat failure is not a path-policy rejection
Defensive patterns

Strategy: retry

Validate before calling

if info, err := os.Stat(path); err != nil { return fmt.Errorf("target unavailable before open: %w", err) }

Try / catch

var pve *fileio.PathValidationError
if errors.As(err, &pve) { /* validation-class failure; inspect pve.Unwrap() errno */ } else if err != nil { /* transport/other */ }

Prevention

When it happens

Trigger: f.Stat() on the *os.File returned by vfs.OpenFile fails after the open succeeded — e.g. the fd was closed concurrently, the file was deleted on a filesystem where the inode vanished mid-call, or an fd/resource limit (EMFILE/ENFILE) hit at fstat time.

Common situations: Concurrent code closing the same *os.File; process hitting RLIMIT_NOFILE; exotic filesystems (FUSE/network mounts) returning EIO on fstat.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/a8c05323b58eb258. Report an issue: GitHub.