go resource_error ai_generated true

rpc error: code = ResourceExhausted desc = grpc: received message larger than max (5242881 vs. 4194304)

ID: go/grpc-resource-exhausted-message-larger-than-max

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

The received message exceeds the default 4 MiB max receive size (grpc.MaxRecvMsgSize). gRPC rejects messages above the configured limit to protect memory. The default is 4 MiB for both send and receive.

generic

中文

接收到的消息超过了默认的 4 MiB 最大接收大小(grpc.MaxRecvMsgSize)。gRPC 会拒绝超过配置限制的消息以保护内存。发送和接收的默认值均为 4 MiB。

Workarounds

  1. 90% success
    Raise the limit deliberately on both ends to a value you can justify:
    
    srv := grpc.NewServer(grpc.MaxRecvMsgSize(16<<20), grpc.MaxSendMsgSize(16<<20))
    conn, _ := grpc.Dial(addr, grpc.WithDefaultCallOptions(
        grpc.MaxCallRecvMsgSize(16<<20),
        grpc.MaxCallSendMsgSize(16<<20)))
  2. 88% success
    For genuinely large payloads, switch to client/server streaming instead of a unary message:
    
    stream, _ := client.Upload(ctx)
    for _, chunk := range chunks { stream.Send(&pb.Chunk{Data: chunk}) }
    _, err := stream.CloseAndRecv()

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The max-size check is applied to the uncompressed message on the receiving side, so compression does not raise the limit and the error persists.

  2. 60% fail

    Allowing unbounded messages exposes the service to OOM from a single malicious or buggy client; the protection exists for a reason.