Reputation: 5260
I have a handler like this:
func (daemon *Daemon) List(item string) (map[string][]string, error) {
panic("this is a panic")
...
I just want to recover this potential panic, so in my grpc server I wrote:
// Serve serves gRPC request by goroutines
func (s *ServerRPC) Serve(addr string) error {
defer func() {
if err := recover(); err != nil {
glog.Errorf("Recovered from err: ", err)
}
}()
l, err := net.Listen("tcp", addr)
if err != nil {
glog.Fatalf("Failed to listen %s: %v", addr, err)
return err
}
return s.server.Serve(l)
}
But it does not capture the panic, I thought it was because my panic happened in child goroutines? If so, how can I recover my grpc server from crashing properly?
Upvotes: 5
Views: 10566
Reputation: 20683
Take a look at the recovery interceptor in the Golang gRPC Middlewares package. It recovers from panics in the request Goroutine and returns them as an internal gRPC error.
myServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_zap.StreamServerInterceptor(zapLogger),
grpc_auth.StreamServerInterceptor(myAuthFunction),
grpc_recovery.StreamServerInterceptor(),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_zap.UnaryServerInterceptor(zapLogger),
grpc_auth.UnaryServerInterceptor(myAuthFunction),
grpc_recovery.UnaryServerInterceptor(),
)),
)
Upvotes: 6