apache/beam · error

error encoding bool: %v

Error message

error encoding bool: %v

What it means

The exec coder's Encode writes booleans as a single byte (1 or 0) via an unsafe fast-path write to the output writer. If that underlying write fails, the error is wrapped as 'error encoding bool'. The failure reflects an I/O problem with the destination stream, not the value itself.

Source

Thrown at sdks/go/pkg/beam/core/runtime/exec/coder.go:349

	fv := &FullValue{}
	if err := d.DecodeTo(r, fv); err != nil {
		return nil, err
	}
	return fv, nil
}

type boolEncoder struct{}

func (*boolEncoder) Encode(val *FullValue, w io.Writer) error {
	// Encoding: false = 0, true = 1
	var err error
	if val.Elm.(bool) {
		_, err = ioutilx.WriteUnsafe(w, []byte{1})
	} else {
		_, err = ioutilx.WriteUnsafe(w, []byte{0})
	}
	if err != nil {
		return fmt.Errorf("error encoding bool: %v", err)
	}
	return nil
}

type boolDecoder struct{}

func (*boolDecoder) DecodeTo(r io.Reader, fv *FullValue) error {
	// Encoding: false = 0, true = 1
	b := make([]byte, 1)
	if err := ioutilx.ReadNBufUnsafe(r, b); err != nil {
		if err == io.EOF {
			return err
		}
		return fmt.Errorf("error decoding bool: %v", err)
	}
	switch b[0] {
	case 0:
		*fv = FullValue{Elm: false}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Inspect the wrapped inner error to identify the underlying I/O failure.
  2. Ensure the destination writer remains open and healthy for the duration of encoding.
  3. Check for earlier stream failures (a broken pipe usually affects all subsequent writes).
  4. Add retry/reconnect logic at the transport layer if writing over the network.

Example fix

// before: encoding into a writer that may be closed
err := enc.Encode(w, val)
// after: verify writer is live and handle wrapped errors
if c, ok := w.(io.Closer); ok && c == closedWriter { return errors.New("writer closed") }
if err := enc.Encode(w, val); err != nil { log.Printf("encode failed: %v", errors.Unwrap(err)) }
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify writer health before encoding:
if w == nil { return errors.New("nil writer") }

Type guard

func writerHealthy(w io.Writer) bool { type c interface{ Close() error }; if cw, ok := w.(c); ok { _ = cw }; return w != nil }

Try / catch

if err := enc.Encode(w, val); err != nil {
    if strings.Contains(err.Error(), "error encoding bool") {
        return fmt.Errorf("transport failure: %w", errors.Unwrap(err))
    }
    return err
}

Prevention

When it happens

Trigger: Encoding a FullValue whose Elm is a bool into a writer that is closed, broken (pipe), or at an I/O error; cascading failures when an earlier write in the stream already failed.

Common situations: Writing to a network/shuffle connection that dropped mid-record; encoding to a closed bytes.Buffer-adjacent writer or file handle; disk-full conditions during pipeline data transport.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/b2798746b91a8977. Report an issue: GitHub.