go runtime_error ai_generated true

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

ID: go/grpc-internal-server-panic

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-01-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active

Root Cause

Server-side code panics due to a nil pointer or other runtime error, causing an Internal error.

generic

中文

服务器端代码因空指针或其他运行时错误而恐慌,导致 Internal 错误。

Workarounds

  1. 95% success
    func recoveryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
        defer func() { if r := recover(); r != nil { log.Printf("panic: %v", r); err = status.Errorf(codes.Internal, "internal error") } }()
        return handler(ctx, req)
    }
  2. 80% success
    if req.User == nil { return nil, status.Error(codes.InvalidArgument, "user required") }

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The bug will recur when the same condition is hit.

  2. 60% fail

    Without logs, you can't identify the root cause.