kubernetes/kops · error

writing body: %w

Error message

writing body: %w

What it means

The marshaled body is written immediately after the header; a failing Write is wrapped as "writing body: %w". Because the header was already flushed, the output file is left with a header and missing/incomplete body — the code notes a TODO about rotating the file, so recovery is left to the caller.

Source

Thrown at pkg/otel/otlptracefile/writer.go:144

	defer w.fileMutex.Unlock()

	if w.f == nil {
		return fmt.Errorf("already closed")
	}

	// write the object with a header.
	header := make([]byte, 16)
	binary.BigEndian.PutUint32(header[0:4], uint32(len(buf)))
	binary.BigEndian.PutUint32(header[4:8], checksum)
	binary.BigEndian.PutUint32(header[8:12], flags)
	binary.BigEndian.PutUint32(header[12:16], uint32(typeCode))

	if _, err := w.f.Write(header); err != nil {
		return fmt.Errorf("writing header: %w", err)
	}
	if _, err := w.f.Write(buf); err != nil {
		// TODO: Rotate file?
		return fmt.Errorf("writing body: %w", err)
	}

	return nil
}

// Close closes the output file.
func (w *writer) Close() error {
	w.fileMutex.Lock()
	defer w.fileMutex.Unlock()

	if w.f != nil {
		if err := w.f.Close(); err != nil {
			return err
		}
		w.f = nil
	}

	return nil

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Free disk space and delete the partially written trace file (it is truncated/corrupt).
  2. Re-run the trace export with adequate storage.
  3. If persistent, check storage device/mount health and consider file rotation.
Defensive patterns

Strategy: retry

Validate before calling

info, err := os.Stat(cfg.Path)
if err == nil && info.Size() > maxTraceFileSize {
    return errors.New("trace file full; rotate before exporting")
}

Try / catch

if err := w.writeObject(ctx, obj); err != nil {
    if strings.Contains(err.Error(), "writing body") {
        // header written but body lost: truncate/recreate the file before retrying
    }
    return err
}

Prevention

When it happens

Trigger: Same I/O conditions as the header write (disk full, device error) but failing on the body write; disk fills between header and body writes.

Common situations: Disk hitting capacity mid-write during heavy span export; flaky network-attached storage.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/4fce444c0ed8c6c1. Report an issue: GitHub.