hashicorp/nomad · error

write error, wrote %d bytes of %d: %v

Error message

write error, wrote %d bytes of %d: %v

What it means

Returned by `writeJSON` when the write of the marshaled JSON bytes to the destination io.Writer (an archive file or stream writer) fails partway. It reports how many bytes were written versus the total, so you can tell whether the archive entry is truncated. The error is returned to the caller, which records it as a capture error in the bundle rather than aborting the whole debug run.

Source

Thrown at command/operator_debug.go:1555

	if err != nil {
		return err
	}
	return nil
}

// writeNDJSON writes a single Nomad API objects (or response error) to the
// archive file as a JSON object.
func writeJSON(obj any, writer io.Writer) error {
	buf, err := json.Marshal(obj)
	if err != nil {
		buf, err = json.Marshal(errorWrapper{Error: err.Error()})
		if err != nil {
			return fmt.Errorf("could not serialize our own error: %v", err)
		}
	}
	n, err := writer.Write(buf)
	if err != nil {
		return fmt.Errorf("write error, wrote %d bytes of %d: %v", n, len(buf), err)
	}
	return nil
}

// writeNDJSON writes a slice of Nomad API objects to the archive file as
// newline-delimited JSON objects.
func writeNDJSON[T any](data []T, writer io.Writer) error {
	for _, obj := range data {
		err := writeJSON(obj, writer)
		if err != nil {
			return fmt.Errorf("failed to write to file: %w", err)
		}
		_, err = writer.Write([]byte{'\n'})
		if err != nil {
			return fmt.Errorf("failed to write to file: %w", err)
		}
	}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Free space on the volume holding the capture directory — ENOSPC is the dominant cause.
  2. Re-run the debug collection after resolving the disk/storage condition; affected archive entries will be truncated.
  3. Target local disk rather than network storage for the -output directory.
  4. Check system logs (dmesg, journald) for filesystem errors that may have remounted the disk read-only.

Example fix

// before (shell)
nomad operator debug -output /mnt/nfs/debug   # nfs dropped
// after
nomad operator debug -output /var/tmp/debug
Defensive patterns

Strategy: retry

Validate before calling

if usage, err := diskusage(outDir); err == nil && usage.Free < estimatedBundleSize {
    return fmt.Errorf("only %d bytes free on %s", usage.Free, outDir)
}

Try / catch

if err := writeJSON(obj, w); err != nil {
    var pe *fs.PathError
    if errors.As(err, &pe) && errors.Is(pe.Err, syscall.ENOSPC) {
        // free space or relocate output, then re-run the capture
    }
    return err
}

Prevention

When it happens

Trigger: writer.Write failing during capture output: disk full (ENOSPC), I/O error on the underlying file, or the writer being closed/consumed prematurely by an earlier failure in the capture pipeline.

Common situations: Debug bundle collection on a node whose disk filled mid-capture; writes to a filesystem that went read-only (disk remounted after error); interrupted network storage.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/42a280defc3fa5d7. Report an issue: GitHub.