{"record":{"id":"14831cc9df77ce40","repo":"microsoft/typescript-go","slug":"failed-to-write-heap-profile-w","errorCode":null,"errorMessage":"failed to write heap profile: %w","messagePattern":"failed to write heap profile: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/pprof/pprof.go","lineNumber":139,"sourceCode":"}\n\n// SaveHeapProfile saves a heap profile to the specified directory.\nfunc SaveHeapProfile(profileDir string) (string, error) {\n\tif err := os.MkdirAll(profileDir, 0o755); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create profile directory: %w\", err)\n\t}\n\n\theapProfilePath := filepath.Join(profileDir, fmt.Sprintf(\"%d-%d-heapprofile.pb.gz\", os.Getpid(), time.Now().UnixMilli()))\n\theapFile, err := os.Create(heapProfilePath)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create heap profile file: %w\", err)\n\t}\n\tdefer heapFile.Close()\n\n\truntime.GC()\n\tif err := pprof.Lookup(\"heap\").WriteTo(heapFile, 0); err != nil {\n\t\tos.Remove(heapProfilePath)\n\t\treturn \"\", fmt.Errorf(\"failed to write heap profile: %w\", err)\n\t}\n\n\treturn heapProfilePath, nil\n}\n\n// SaveAllocProfile saves an allocation profile to the specified directory.\nfunc SaveAllocProfile(profileDir string) (string, error) {\n\tif err := os.MkdirAll(profileDir, 0o755); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create profile directory: %w\", err)\n\t}\n\n\tallocProfilePath := filepath.Join(profileDir, fmt.Sprintf(\"%d-%d-allocprofile.pb.gz\", os.Getpid(), time.Now().UnixMilli()))\n\tallocFile, err := os.Create(allocProfilePath)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create alloc profile file: %w\", err)\n\t}\n\tdefer allocFile.Close()\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/microsoft/typescript-go/blob/1bcfa18d79a3be41772223d5c05dfe4480e614ff/internal/pprof/pprof.go#L121-L157","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure free disk at least the size of the live heap before dumping","Write the profile to local scratch, then copy remotely","Retry once after freeing space; transient EIO on network mounts usually clears"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// require free space roughly equal to live heap before dumping\nvar ms runtime.MemStats\nruntime.ReadMemStats(&ms)\nif free := diskFree(profileDir); free < int64(ms.HeapAlloc) {\n\treturn fmt.Errorf(\"insufficient disk (%d) for heap ~%d\", free, ms.HeapAlloc)\n}","typeGuard":null,"tryCatchPattern":"path, err := pprof.SaveHeapProfile(dir)\nif err != nil && strings.Contains(err.Error(), \"failed to write heap profile\") {\n\ttime.Sleep(500 * time.Millisecond) // transient EIO / ENOSPC relief\n\tpath, err = pprof.SaveHeapProfile(dir)\n}\nif err != nil { return \"\", err }","preventionTips":["Dump to local disk first, then copy off-host","Free space before requesting dumps during incidents","Avoid FUSE/network mounts for large profile writes"],"tags":["pprof","heap","io","disk-full"],"backgroundTag":null,"analyzedSha":"1bcfa18d79a3be41772223d5c05dfe4480e614ff","analyzedAt":"2026-08-16T02:12:00.115Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}