go-delve/delve · error

error writing output file: %v

Error message

error writing output file: %v

What it means

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.

Source

Thrown at pkg/proc/dump.go:104

	state.Mutex.Unlock()
}

func (state *DumpState) isCanceled() bool {
	state.Mutex.Lock()
	defer state.Mutex.Unlock()
	return state.Canceled
}

// Dump writes a core dump to out. State is updated as the core dump is written.
func (t *Target) Dump(out elfwriter.WriteCloserSeeker, flags DumpFlags, state *DumpState) {
	defer func() {
		state.Mutex.Lock()
		if ierr := recover(); ierr != nil {
			state.Err = newInternalError(ierr, 2)
		}
		err := out.Close()
		if state.Err == nil && err != nil {
			state.Err = fmt.Errorf("error writing output file: %v", err)
		}
		state.Dumping = false
		state.Mutex.Unlock()
		if state.DoneChan != nil {
			close(state.DoneChan)
		}
	}()

	bi := t.BinInfo()

	var fhdr elf.FileHeader
	fhdr.Class = elf.ELFCLASS64
	fhdr.Data = elf.ELFDATA2LSB
	fhdr.Version = elf.EV_CURRENT

	switch bi.GOOS {
	case "linux":
		fhdr.OSABI = elf.ELFOSABI_LINUX

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Check free disk space on the destination filesystem and rerun the dump to a volume with enough room for the full core.
  2. Verify no other code path closes the writer passed to Dump before Dump finishes (wait on state.DoneChan / state.AllDone before closing).
  3. Inspect the wrapped %v error for the concrete errno (ENOSPC, EIO, EBADF) and address it specifically.
  4. Write the dump to a local disk instead of a network/remounted filesystem.
  5. Check file permissions and that the destination path is writable.

Example fix

// before
dstate.Dump(f, proc.DumpFlags(0), state)
f.Close()

// after
dstate.Dump(f, proc.DumpFlags(0), state) // Dump closes f itself via out.Close()
<-state.DoneChan // or poll state.AllDone
if state.Err != nil {
    log.Fatalf("core dump failed: %v", state.Err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: check writable destination and free space before dumping
func canWriteDump(path string, need uint64) error {
    var st syscall.Statfs_t
    if err := syscall.Statfs(filepath.Dir(path), &st); err != nil {
        return err
    }
    if uint64(st.Bavail)*uint64(st.Bsize) < need {
        return fmt.Errorf("insufficient space for %d byte dump", need)
    }
    f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
    if err != nil { return err }
    return f.Close()
}

Type guard

func dumpFailed(state *proc.DumpState) bool {
    state.Mutex.Lock()
    defer state.Mutex.Unlock()
    return state.Err != nil
}

Try / catch

f, err := os.Create(outPath)
if err != nil { return err }
state := &proc.DumpState{DoneChan: make(chan struct{})}
target.Dump(f, proc.DumpFlags(0), state)
<-state.DoneChan // Dump closes f; a Close error surfaces here
if state.Err != nil {
    if strings.Contains(state.Err.Error(), "error writing output file") {
        os.Remove(outPath)
    }
    return state.Err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/19f18fd420681402. Report an issue: GitHub.