go runtime_error ai_generated true

panic: runtime error: invalid memory address or nil pointer dereference

ID: go/grpc-unary-interceptor-nil-response

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2025-03-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

A unary interceptor returned nil error but left the reply message nil, or the handler returned a nil response with a nil error. The caller then dereferences the nil reply.

generic

中文

一元拦截器返回了 nil 错误但将回复消息留为 nil,或处理函数返回了 nil 响应和 nil 错误。调用方随后解引用该 nil 回复。

Workarounds

  1. 92% success
    Ensure handlers always return a non-nil message with nil error: return &pb.UserResponse{...}, nil; and validate in interceptors that reply != nil before returning.
  2. 85% success
    Use protoc-gen-validate or manual checks to guarantee non-nil responses, and add a unit test that asserts the handler never returns (nil, nil).

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Recovering hides the nil reply and returns a zero-value to the caller, which then processes garbage.

  2. 75% fail

    The panic may occur inside the server handler or interceptor chain before the response is sent.