golang/go · error

failed to stat trace file: %v

Error message

failed to stat trace file: %v

What it means

The `go tool trace` command successfully opened the trace file but `os.File.Stat()` failed on the open file handle. The stat result is used to determine the file size (`traceSize`) which is needed for subsequent trace parsing. A stat failure on an already-open file is unusual and typically indicates a filesystem-level problem.

Source

Thrown at src/cmd/trace/main.go:105

	case 1:
		traceFile = flag.Arg(0)
	case 2:
		programBinary = flag.Arg(0)
		traceFile = flag.Arg(1)
	default:
		flag.Usage()
	}

	tracef, err := os.Open(traceFile)
	if err != nil {
		logAndDie(fmt.Errorf("failed to read trace file: %w", err))
	}
	defer tracef.Close()

	// Get the size of the trace file.
	fi, err := tracef.Stat()
	if err != nil {
		logAndDie(fmt.Errorf("failed to stat trace file: %v", err))
	}
	traceSize := fi.Size()

	// Handle requests for profiles.
	if *pprofFlag != "" {
		parsed, err := parseTrace(tracef, traceSize)
		if err != nil {
			logAndDie(err)
		}
		var f traceviewer.ProfileFunc
		switch *pprofFlag {
		case "net":
			f = pprofByGoroutine(computePprofIO(), parsed)
		case "sync":
			f = pprofByGoroutine(computePprofBlock(), parsed)
		case "syscall":
			f = pprofByGoroutine(computePprofSyscall(), parsed)
		case "sched":

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Copy the trace file to local storage (e.g., /tmp) and retry: `cp trace.out /tmp/ && go tool trace /tmp/trace.out`.
  2. If on a network filesystem, ensure the connection is stable and remount if necessary.
  3. Verify the file still exists and is accessible: `ls -la <trace_file> && stat <trace_file>`.
  4. Regenerate the trace file if it may have been corrupted or partially deleted.
  5. Check system logs (`dmesg`, `/var/log/syslog`) for filesystem errors.

Example fix

# Before: trace file on unstable network filesystem
go tool trace /nfs/share/trace.out

# After: copy to local storage first
cp /nfs/share/trace.out /tmp/trace.out
go tool trace /tmp/trace.out
Defensive patterns

Strategy: validation

Validate before calling

// Validate trace file stability before analysis (avoid network FS issues)
info, err := os.Stat(traceFile)
if err != nil {
    log.Fatalf("Cannot stat trace file: %v", err)
}
// If on network filesystem, copy to local first
if isNetworkMount(traceFile) {
    localCopy := filepath.Join(os.TempDir(), filepath.Base(traceFile))
    if err := copyFile(traceFile, localCopy); err != nil {
        log.Fatalf("Cannot copy trace to local storage: %v", err)
    }
    traceFile = localCopy
}

Prevention

When it happens

Trigger: Fires at trace/main.go:103-105 when `tracef.Stat()` returns an error after the file was successfully opened. The file handle is already open (open succeeded at line 96). `traceSize` from the stat is passed to `parseTrace` later for size-bounded processing.

Common situations: The file was deleted between the open and stat calls (race condition). The file is on a network filesystem (NFS, CIFS) that became unreachable. A FUSE filesystem that doesn't support stat on open files. The file descriptor was closed by another goroutine (concurrency issue). OS-level file handle corruption.

Related errors


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