micro/go-micro · critical

grpc: received message larger than max length allowed on cur

Error message

grpc: received message larger than max length allowed on current machine (%d vs. %d)

What it means

The gRPC codec's decode function reads a 4-byte length-prefixed frame and validates the declared length before allocating. If the declared message length exceeds the maximum representable int on the current platform (int64(length) > int64(maxInt), i.e. a corrupt or malicious prefix on a 32-bit platform), this error is returned instead of attempting a doomed allocation.

Source

Thrown at codec/grpc/util.go:35

	// read the header
	if _, err := r.Read(header); err != nil {
		return uint8(0), nil, err
	}

	// get encoding format e.g compressed
	cf := header[0]

	// get message length
	length := binary.BigEndian.Uint32(header[1:])

	// no encoding format
	if length == 0 {
		return cf, nil, nil
	}

	//
	if int64(length) > int64(maxInt) {
		return cf, nil, fmt.Errorf("grpc: received message larger than max length allowed on current machine (%d vs. %d)", length, maxInt)
	}
	if int(length) > MaxMessageSize {
		return cf, nil, fmt.Errorf("grpc: received message larger than max (%d vs. %d)", length, MaxMessageSize)
	}

	msg := make([]byte, int(length))

	if _, err := r.Read(msg); err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return cf, nil, err
	}

	return cf, msg, nil
}

func encode(cf uint8, buf []byte, w io.Writer) error {

View on GitHub (pinned to 24529f1404)

Solutions

  1. Verify both peers use the same protocol/framing (real gRPC vs this codec).
  2. Check for proxies/middlewares corrupting or re-chunking the response stream.
  3. Reset the connection after desync; the stream is unrecoverable.
  4. On 32-bit platforms, prefer exchanging messages whose lengths fit comfortably in int; upgrade to 64-bit if huge payloads are legitimate.
Defensive patterns

Strategy: try-catch

Validate before calling

// Before reading, cap expected sizes in application logic:
const maxExpected = 1 << 30
// ensure the peer is a real gRPC-framed endpoint before interpreting bytes as frames

Try / catch

cf, buf, err := decode(r)
if err != nil {
	if strings.HasPrefix(err.Error(), "grpc: received message larger than max length allowed") {
		// corrupt frame / protocol desync: close and reconnect
	}
	return err
}

Prevention

When it happens

Trigger: Receiving a gRPC frame whose 4-byte big-endian length prefix decodes to a value larger than the platform's max int (int overflow guard); essentially always means a corrupt stream or a non-gRPC peer on the wire.

Common situations: Connecting a client to a server that speaks a different framing protocol; truncated/garbled TLS or proxy responses being parsed as gRPC frames; bit corruption or desynced stream after an earlier partial read.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/7dcee1fce4db9fae. Report an issue: GitHub.