go
runtime_error
ai_generated
true
rpc 错误:code = Internal desc = grpc: 服务器无法编码响应:rpc 错误:code = Internal desc = grpc: 编组时出错:proto: 使用 nil 调用 Marshal
rpc error: code = Internal desc = grpc: server failed to encode response: rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil
ID: go/grpc-internal-error-server-panic
80%修复率
85%置信度
0证据数
2024-09-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.x | active | — | — | — |
根因分析
服务器处理程序返回了 nil 响应消息,gRPC 框架尝试编组它,导致 panic 或错误。
English
The server handler returned a nil response message, and the gRPC framework attempted to marshal it, causing a panic or error.
解决方案
-
95% 成功率
func (s *server) YourMethod(ctx context.Context, req *pb.Request) (*pb.Response, error) { // Always return a valid response return &pb.Response{}, nil } -
90% 成功率
func validateResponseInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { resp, err := handler(ctx, req) if err != nil { return nil, err } if resp == nil { return nil, status.Error(codes.Internal, "nil response") } return resp, nil }
无效尝试
常见但无效的做法:
-
70% 失败
Recovering from the panic without fixing the nil response still results in an error sent to the client.
-
50% 失败
Returning an empty message may be semantically incorrect and does not address the root cause.