grpc unhandled exception StatusCode=Unknown when invoking a method generated from a proto file
Asked Answered
C

1

12

I have a c# client and server and get an unhandled exception of type Grpc.Core.RpcException with Status(StatusCode=Unknown, Detail="Exception was thrown by handler.") when invoking the SendMessage function in client (generated from a proto file).

The stack trace is:

Got response: True
RPC failed Grpc.Core.RpcException: Status(StatusCode=Unknown, Detail="Exception was thrown by handler.")
   at Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg) in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\AsyncCall.cs:line 75
   at Grpc.Core.DefaultCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in T:\src\github\grpc\src\csharp\Grpc.Core\DefaultCallInvoker.cs:line 46
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx) in T:\src\github\grpc\src\csharp\Grpc.Core\Interceptors\InterceptingCallInvoker.cs:line 51
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) in T:\src\github\grpc\src\csharp\Grpc.Core\ClientBase.cs:line 174
   at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in T:\src\github\grpc\src\csharp\Grpc.Core\Interceptors\InterceptingCallInvoker.cs:line 48
   at MessageService.MessageServiceClient.SendMessage(Message request, CallOptions options) in C:\Work\Dev\sphirra\hub-communication\message-hub\Hub-Messenger\MessageGrpc.cs:line 95
   at MessageService.MessageServiceClient.SendMessage(Message request, Metadata headers, Nullable`1 deadline, CancellationToken cancellationToken) in C:\Work\Dev\sphirra\hub-communication\message-hub\Hub-Messenger\MessageGrpc.cs:line 91

The sendMessage method in the proto file is as follows:

service MessageService{

rpc SendMessage(Message) returns (Empty){}
}

message Message{
    bytes msg = 1;
    int32 senderId = 2;
    int32 receiverId = 3;
}

message Empty {}

The SendMessage implementation in Client:

public void SendMessage(String msg, int receiverId)
            {
                try
                {
                    client.SendMessage(NewMsg(msg, ID, receiverId)); //this is where it gets stuck
                    Log("sending: ", msg);
                }
                catch (RpcException e)
                {
                    Log("RPC failed " + e);
                    throw;
                }
            }

The server-side implementation of the method:

public override Task<Empty> SendMessage(Message request, ServerCallContext context)
        {
            try
            {
                Task<Empty> empty = null;
                messages.Add(request);
                return empty;
            }
            catch (RpcException e){
                Console.WriteLine("Remote procedure call failed: " + e);
                throw;
            }

        }
Camisole answered 23/10, 2018 at 14:16 Comment(1)
Any output from the server? This error usually happens when your handler code throws an exception that is not caught. Maybe try to wrap the entire SendMessage() server handler in a big try-catch-all block and see if any exceptions are thrown.Fighterbomber
A
1

In my case, the issue was not wrapping object in Task.FromResult() while returning it.

Old code:

return new Object{};

Corrected Code

return Task.fromResult(new Object{});

Also always avoid null object or null properties in objects in return statement.

Aloisia answered 6/11, 2023 at 7:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.