{"record":{"id":"19f18fd420681402","repo":"go-delve/delve","slug":"error-writing-output-file-v","errorCode":null,"errorMessage":"error writing output file: %v","messagePattern":"error writing output file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/dump.go","lineNumber":104,"sourceCode":"\tstate.Mutex.Unlock()\n}\n\nfunc (state *DumpState) isCanceled() bool {\n\tstate.Mutex.Lock()\n\tdefer state.Mutex.Unlock()\n\treturn state.Canceled\n}\n\n// Dump writes a core dump to out. State is updated as the core dump is written.\nfunc (t *Target) Dump(out elfwriter.WriteCloserSeeker, flags DumpFlags, state *DumpState) {\n\tdefer func() {\n\t\tstate.Mutex.Lock()\n\t\tif ierr := recover(); ierr != nil {\n\t\t\tstate.Err = newInternalError(ierr, 2)\n\t\t}\n\t\terr := out.Close()\n\t\tif state.Err == nil && err != nil {\n\t\t\tstate.Err = fmt.Errorf(\"error writing output file: %v\", err)\n\t\t}\n\t\tstate.Dumping = false\n\t\tstate.Mutex.Unlock()\n\t\tif state.DoneChan != nil {\n\t\t\tclose(state.DoneChan)\n\t\t}\n\t}()\n\n\tbi := t.BinInfo()\n\n\tvar fhdr elf.FileHeader\n\tfhdr.Class = elf.ELFCLASS64\n\tfhdr.Data = elf.ELFDATA2LSB\n\tfhdr.Version = elf.EV_CURRENT\n\n\tswitch bi.GOOS {\n\tcase \"linux\":\n\t\tfhdr.OSABI = elf.ELFOSABI_LINUX","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/dump.go#L86-L122","documentation":"This error is set in the deferred cleanup of (*Target).Dump when closing the output writer (out.Close()) returns an error after the dump itself completed without recording another error. Delve generates ELF core dumps by streaming into a caller-supplied elfwriter.WriteCloserSeeker; if flushing/closing the underlying file fails (the dump state did not already hold an error), this wrapped error is stored in DumpState.Err. It means the core dump file on disk is likely incomplete or corrupt even though the in-memory dump generation seemed to succeed.","triggerScenarios":"Calling (*Target).Dump(out, flags, state) where out.Close() returns a non-nil error at the end of the dump — e.g. the underlying *os.File hit ENOSPC mid-stream, was closed elsewhere, or a buffered writer's Flush failed.","commonSituations":"Disk filled up while a large core dump was being written; the user or another goroutine closed the output file prematurely; writing to a network mount or removable disk that disconnected; a bufio.Writer wrapping the file fails to flush on Close.","solutions":["Check free disk space on the destination filesystem and rerun the dump to a volume with enough room for the full core.","Verify no other code path closes the writer passed to Dump before Dump finishes (wait on state.DoneChan / state.AllDone before closing).","Inspect the wrapped %v error for the concrete errno (ENOSPC, EIO, EBADF) and address it specifically.","Write the dump to a local disk instead of a network/remounted filesystem.","Check file permissions and that the destination path is writable."],"exampleFix":"// before\ndstate.Dump(f, proc.DumpFlags(0), state)\nf.Close()\n\n// after\ndstate.Dump(f, proc.DumpFlags(0), state) // Dump closes f itself via out.Close()\n<-state.DoneChan // or poll state.AllDone\nif state.Err != nil {\n    log.Fatalf(\"core dump failed: %v\", state.Err)\n}","handlingStrategy":"try-catch","validationCode":"// Go: check writable destination and free space before dumping\nfunc canWriteDump(path string, need uint64) error {\n    var st syscall.Statfs_t\n    if err := syscall.Statfs(filepath.Dir(path), &st); err != nil {\n        return err\n    }\n    if uint64(st.Bavail)*uint64(st.Bsize) < need {\n        return fmt.Errorf(\"insufficient space for %d byte dump\", need)\n    }\n    f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)\n    if err != nil { return err }\n    return f.Close()\n}","typeGuard":"func dumpFailed(state *proc.DumpState) bool {\n    state.Mutex.Lock()\n    defer state.Mutex.Unlock()\n    return state.Err != nil\n}","tryCatchPattern":"f, err := os.Create(outPath)\nif err != nil { return err }\nstate := &proc.DumpState{DoneChan: make(chan struct{})}\ntarget.Dump(f, proc.DumpFlags(0), state)\n<-state.DoneChan // Dump closes f; a Close error surfaces here\nif state.Err != nil {\n    if strings.Contains(state.Err.Error(), \"error writing output file\") {\n        os.Remove(outPath)\n    }\n    return state.Err\n}","preventionTips":["Ensure free disk space exceeds state.MemTotal before starting the dump.","Never close the writer yourself; Dump closes it via out.Close().","Wait for state.DoneChan (or state.AllDone) before inspecting or removing the file.","Always check state.Err after completion, not just the absence of a panic.","Prefer local disks over network mounts for large core dumps."],"tags":["go","core-dump","file-io","disk-space"],"backgroundTag":"core-dump-write-failed","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}