microsoft/typescript-go · error

failed to write heap profile: %w

Error message

failed to write heap profile: %w

What it means

SaveHeapProfile ran runtime.GC() and then pprof.Lookup("heap").WriteTo failed while serializing the heap profile to the open file. The file is closed via defer and the partial artifact is removed, leaving no corrupt file. Write errors here are I/O-level (ENOSPC mid-write, EIO) or extremely rarely an encoder failure on a torn heap-iteration state.

Source

Thrown at internal/pprof/pprof.go:139

}

// SaveHeapProfile saves a heap profile to the specified directory.
func SaveHeapProfile(profileDir string) (string, error) {
	if err := os.MkdirAll(profileDir, 0o755); err != nil {
		return "", fmt.Errorf("failed to create profile directory: %w", err)
	}

	heapProfilePath := filepath.Join(profileDir, fmt.Sprintf("%d-%d-heapprofile.pb.gz", os.Getpid(), time.Now().UnixMilli()))
	heapFile, err := os.Create(heapProfilePath)
	if err != nil {
		return "", fmt.Errorf("failed to create heap profile file: %w", err)
	}
	defer heapFile.Close()

	runtime.GC()
	if err := pprof.Lookup("heap").WriteTo(heapFile, 0); err != nil {
		os.Remove(heapProfilePath)
		return "", fmt.Errorf("failed to write heap profile: %w", err)
	}

	return heapProfilePath, nil
}

// SaveAllocProfile saves an allocation profile to the specified directory.
func SaveAllocProfile(profileDir string) (string, error) {
	if err := os.MkdirAll(profileDir, 0o755); err != nil {
		return "", fmt.Errorf("failed to create profile directory: %w", err)
	}

	allocProfilePath := filepath.Join(profileDir, fmt.Sprintf("%d-%d-allocprofile.pb.gz", os.Getpid(), time.Now().UnixMilli()))
	allocFile, err := os.Create(allocProfilePath)
	if err != nil {
		return "", fmt.Errorf("failed to create alloc profile file: %w", err)
	}
	defer allocFile.Close()

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Ensure free disk at least the size of the live heap before dumping
  2. Write the profile to local scratch, then copy remotely
  3. Retry once after freeing space; transient EIO on network mounts usually clears
Defensive patterns

Strategy: retry

Validate before calling

// require free space roughly equal to live heap before dumping
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
if free := diskFree(profileDir); free < int64(ms.HeapAlloc) {
	return fmt.Errorf("insufficient disk (%d) for heap ~%d", free, ms.HeapAlloc)
}

Try / catch

path, err := pprof.SaveHeapProfile(dir)
if err != nil && strings.Contains(err.Error(), "failed to write heap profile") {
	time.Sleep(500 * time.Millisecond) // transient EIO / ENOSPC relief
	path, err = pprof.SaveHeapProfile(dir)
}
if err != nil { return "", err }

Prevention

When it happens

Trigger: Disk fills exactly during the write of a large heap dump (heaps are often hundreds of MB serialized); backend storage (NFS/FUSE) failing mid-write; process under OOM pressure truncating buffered writes; device error.

Common situations: Heap dumps requested because memory is high — the same pressure that fills the disk kills the dump; ephermal storage quotas in containers; FUSE-based mounts with size caps.

Related errors


AI-assisted analysis of microsoft/typescript-go@1bcfa18d79 (2026-08-16). Data as JSON: /api/errors/14831cc9df77ce40. Report an issue: GitHub.