# INTERNAL: 一元客户端拦截器中捕获到恐慌：运行时错误：无效内存地址或空指针解引用

- **ID:** `grpc/client-interceptor-panic-recovery`
- **领域:** grpc
- **类别:** runtime_error
- **错误码:** `GRPC_INTERNAL_PANIC_INTERCEPTOR`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

客户端拦截器因空指针解引用而恐慌，被 gRPC 内部恢复拦截器捕获，但未在上游妥善处理。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC-go v1.63.0 | active | — | — |
| gRPC-java v1.62.0 | active | — | — |
| envoy v1.29.0 | active | — | — |

## 解决方案

1. ```
   Add explicit nil checks before dereferencing context values in the interceptor. For example, in Go: if md, ok := metadata.FromIncomingContext(ctx); ok { ... } else { return nil, status.Error(codes.Internal, "missing metadata") }
   ```
2. ```
   Wrap the interceptor with a custom recovery middleware that converts panics to gRPC errors with stack traces: use grpc_recovery.UnaryServerInterceptor() from go-grpc-middleware.
   ```
3. ```
   Upgrade to gRPC v1.64.0+ which includes improved panic recovery in interceptors, and enable verbose logging with GRPC_GO_LOG_SEVERITY_LEVEL=info to capture stack traces.
   ```

## 无效尝试

- **** — Restarting the client process without fixing the interceptor code will cause the same panic on the next request. (95% 失败率)
- **** — Adding a generic recovery handler at the application level that logs but returns a generic error masks the root cause, leading to silent data corruption. (85% 失败率)
- **** — Disabling all interceptors removes tracing and auth, but the underlying nil pointer might still exist in the call path. (70% 失败率)
