kubernetes/kops · error

writing header: %w

Error message

writing header: %w

What it means

After passing the closed check, the 16-byte frame header (length, CRC32C checksum, flags, typeCode) is written to the open file. Any Write failure is wrapped as "writing header: %w". A partial/garbled trace file may result since the header precedes the body.

Source

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

	flags := uint32(0)

	w.fileMutex.Lock()
	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
		}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Free disk space or point the trace file at storage with sufficient capacity.
  2. Check host/container storage health and mount state.
  3. Retry the export after fixing I/O; note the trace file may be truncated/corrupt and should be regenerated.
Defensive patterns

Strategy: retry

Validate before calling

if st, err := os.Stat(filepath.Dir(cfg.Path)); err != nil || !st.IsDir() {
    return errors.New("trace output directory unavailable")
}
// check writable
test, _ := os.CreateTemp(filepath.Dir(cfg.Path), ".")
if test != nil { test.Close(); os.Remove(test.Name()) }

Try / catch

if err := w.writeObject(ctx, obj); err != nil {
    if strings.Contains(err.Error(), "writing header") {
        // treat file as corrupt; close, free disk, recreate writer and retry
    }
    return err
}

Prevention

When it happens

Trigger: Disk full; file descriptor closed externally; I/O error on the underlying filesystem while exporting spans.

Common situations: Node running out of disk space during a long test; container with a small tmpfs where the trace file lives; storage device errors.

Related errors


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