apache/beam · critical

panic(err)

Error message

panic(err)

What it means

top's package init registers a coder for the accum type; accumEnc builds a slice-of-bytes encoder via coder.EncoderForSlice. If constructing that encoder fails (unsupported element type for the slice coder), the package panics at init time with the returned error. This is an internal invariant failure, not something user data normally triggers.

Solutions

  1. Run 'go build ./...' and update apache/beam sdks to a consistent, released version (go get -u github.com/apache/beam/sdks/v2/go/...).
  2. Check that the beam coder package is unmodified (no vendored patches) and matches the transforms/top package.
  3. Reproduce with a minimal program importing top; if it panics at init on a stock version, file a Beam JIRA with the version.

Example fix

// before
go.mod: github.com/apache/beam/sdks/v2 v2.45.0 mixed with v2.54.0 packages

// after
go get github.com/apache/beam/sdks/v2/go@v2.54.0 && go mod tidy
Defensive patterns

Strategy: fallback

Validate before calling

// package-level: verify a program importing top initializes in CI
func TestTopInit(t *testing.T) {
    _ = top.Largest // forces package init; a panic here fails the test
}

Try / catch

defer func() {
    if r := recover(); r != nil {
        log.Fatalf("beam top package failed to init (SDK version skew?): %v", r)
    }
}()

Prevention

When it happens

Trigger: An unexpected failure in coder.EncoderForSlice for [][]byte while the top package initializes (init calls accumEnc via beam.RegisterCoder). Practically only triggered by a Beam SDK/coder registry bug or version incompatibility.

Common situations: Broken or mismatched Beam Go SDK installation; vendored dependency version skew where the coder package changed behavior; custom forks/patches to the coder package.

Related errors


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

Appendix: source

Thrown at sdks/go/pkg/beam/transforms/top/top.go:154

		a.list = append(a.list, element)
	}
	a.data = nil
	return nil
}

var (
	accumType = reflect.TypeOf((*accum)(nil)).Elem()
)

func init() {
	beam.RegisterType(accumType)
	beam.RegisterCoder(accumType, accumEnc(), accumDec())
}

func accumEnc() func(accum) ([]byte, error) {
	byteEnc, err := coder.EncoderForSlice(reflect.TypeOf((*[][]byte)(nil)).Elem())
	if err != nil {
		panic(err)
	}
	return func(a accum) ([]byte, error) {
		if len(a.list) > 0 && a.enc == nil {
			return nil, errors.Errorf("top.accum: element encoder unspecified with non-zero elements: %v data available", len(a.data))
		}
		var values [][]byte
		if len(a.list) == 0 && len(a.data) > 0 {
			values = a.data
		}
		for _, value := range a.list {
			var buf bytes.Buffer
			if err := a.enc.Encode(value, &buf); err != nil {
				return nil, errors.WithContextf(err, "top.accum: marshalling %v", value)
			}
			values = append(values, buf.Bytes())
		}

		var buf bytes.Buffer

View on GitHub (pinned to 12126d8942)