apache/beam · error

No data

Error message

No data

What it means

ReadN reads exactly n bytes from an io.Reader by looping over Read calls. If the very first Read returns 0 bytes with no error (EOF on an empty stream), there is 'No data' to decode, so it errors rather than returning a zero-length result.

Source

Thrown at sdks/go/pkg/beam/core/util/ioutilx/read.go:40

	"github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
)

// ReadN reads exactly N bytes from the reader. Fails otherwise.
func ReadN(r io.Reader, n int) ([]byte, error) {
	ret := make([]byte, n)
	index := 0

	for {
		i, err := r.Read(ret[index:])
		if i+index == n {
			return ret, nil
		}
		if err != nil {
			return nil, err
		}
		if i == 0 {
			return nil, errors.New("No data")
		}

		index += i
	}
}

// ReadNBufUnsafe reads exactly cap(buf) bytes from the reader. Fails otherwise.
// Uses the unsafe package unsafely to convince escape analysis that the passed
// in []byte doesn't escape this function through the io.Reader.
// Intended for use with small, fixed sized, stack allocated buffers that have
// no business being allocated to the heap.
// If the io.Reader somehow retains the passed in []byte, then this should not
// be used, and ReadN preferred.
func ReadNBufUnsafe(r io.Reader, b []byte) error {
	// Use with parameter retaining readers at your own peril.
	ret := *(*[]byte)(noescape(unsafe.Pointer(&b)))
	index := 0
	n := len(ret)

View on GitHub (pinned to 12126d8942)

Solutions

  1. Stop decoding when this error occurs — it signals end-of-stream, not corruption
  2. Check the stream/file is not empty or truncated before decoding
  3. Ensure the producer encoded the expected number of elements
  4. Treat it as io.EOF-equivalent in your decode loop

Example fix

// before
for {
    v, err := ioutilx.ReadN(r, n)
    if err != nil { return err }
}
// after
for {
    v, err := ioutilx.ReadN(r, n)
    if err != nil {
        if err.Error() == "No data" || err == io.EOF { return nil } // clean end of stream
        return err
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check stream has data before decoding
if f == nil { return errors.New("nil reader") }
if fi, err := f.Stat(); err == nil && fi.Size() == 0 { return io.EOF }

Try / catch

data, err := ioutilx.ReadN(r, n)
if err != nil {
    if err.Error() == "No data" || errors.Is(err, io.EOF) { return io.EOF }
    return err
}

Prevention

When it happens

Trigger: Calling ReadN on a reader positioned at EOF (empty stream), e.g. decoding an element from an exhausted byte stream via DecodeBytes/DecodeTo or makeReStream.

Common situations: Decoding more elements than were encoded; truncated or empty input files/streams; loop over-decoding past the last element of a coded stream.

Related errors


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