go runtime_error ai_generated true

rpc error: code = Unknown desc = 运行时错误:无效的内存地址或空指针解引用

rpc error: code = Unknown desc = runtime error: invalid memory address or nil pointer dereference

ID: go/grpc-unknown-non-status-error

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2025-04-18首次发现

版本兼容性

版本状态引入弃用备注
1.0+ active — — —

根因分析

服务端处理程序发生 panic。gRPC 恢复 panic 并返回 Unknown,但 panic 消息泄漏到错误描述中。真正的根因是处理程序中的空指针解引用。

English

The server handler panicked. gRPC recovers the panic and returns Unknown, but the panic message leaks into the error description. The real cause is a nil pointer dereference in the handler.

generic

解决方案

  1. 90% 成功率
    func recovery(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
      defer func() {
        if r := recover(); r != nil {
          log.Printf("panic in %s: %v\n%s", info.FullMethod, r, debug.Stack())
        }
      }()
      return handler(ctx, req)
    }
  2. 95% 成功率
    if req == nil || req.User == nil {
      return nil, status.Error(codes.InvalidArgument, "user is required")
    }

无效尝试

常见但无效的做法:

  1. 90% 失败

    The server will panic again for the same input; retries just amplify load and log noise

  2. 95% 失败

    The panic happened on the server; client-side recover cannot catch it