pion/webrtc · error

stream is nil

Error message

stream is nil

What it means

errNilStream is returned by oggreader's NewWith (and similarly ivfreader.NewWith) when the io.Reader passed in is a nil interface. The library cannot read from a nil stream, so it fails fast instead of panicking on the first Read. This is purely a caller-side setup mistake.

Source

Thrown at pkg/media/oggreader/oggreader.go:24

import (
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"strings"
)

const (
	pageHeaderTypeBeginningOfStream = 0x02
	pageHeaderSignature             = "OggS"

	idPageBasePayloadLength = 19
	pageHeaderLen           = 27
)

var (
	errNilStream                       = errors.New("stream is nil")
	errBadIDPageSignature              = errors.New("bad header signature")
	errBadOpusTagsSignature            = errors.New("bad opus tags signature")
	errBadIDPageType                   = errors.New("wrong header, expected beginning of stream")
	errBadIDPageLength                 = errors.New("payload for id page must be 19 bytes")
	errBadIDPagePayloadSignature       = errors.New("bad payload signature")
	errShortPageHeader                 = errors.New("not enough data for payload header")
	errChecksumMismatch                = errors.New("expected and actual checksum do not match")
	errUnsupportedChannelMappingFamily = errors.New("unsupported channel mapping family")
)

// OggReader is used to read Ogg files and return page payloads.
type OggReader struct {
	stream               io.Reader
	bytesReadSuccesfully int64
	checksumTable        *[256]uint32
	doChecksum           bool
}

View on GitHub (pinned to 8c25dc09fa)

Solutions

  1. Ensure a non-nil io.Reader is constructed before calling NewWith
  2. Check the error from whatever produces the reader instead of passing its nil result through
  3. Use os.Open / bytes.NewReader explicitly and verify non-nil
  4. In code paths, guard `if r == nil` before handing the reader to the library

Example fix

// before
var r io.Reader
ogg, err := oggreader.NewWith(r) // errNilStream
// after
f, err := os.Open("audio.ogg")
if err != nil { return err }
ogg, err := oggreader.NewWith(f)
Defensive patterns

Strategy: validation

Validate before calling

if stream == nil {
    return errors.New("oggreader: input stream must be non-nil")
}
ogg, _, err := oggreader.NewWith(stream)

Type guard

func isNonNilReader(r io.Reader) bool { return r != nil }

Try / catch

ogg, _, err := oggreader.NewWith(stream)
if err != nil {
    return fmt.Errorf("failed to init ogg reader: %w", err)
}

Prevention

When it happens

Trigger: Calling oggreader.NewWith(nil) or passing a typed-nil reader (e.g. var r *bytes.Reader; NewWith(r) where r is nil), typically when a variable failed to initialize earlier.

Common situations: Functions that return (io.Reader, error) where the error path returns nil but the caller ignores the error; deferred initialization of a file handle; refactors where a reader field was never assigned.

Related errors


AI-assisted analysis of pion/webrtc@8c25dc09fa (2026-09-03). Data as JSON: /api/errors/afc83cf763e1988e. Report an issue: GitHub.