grpc/grpc-dotnet

SocketException at client when server throws an exception with a long message

bhaeussermann opened this issue · 4 comments

What version of gRPC and what language are you using?

  • Google.Protobuf 3.26.1
  • Grpc.Net.Client 2.62.0
  • Grpc.Tools 2.63.0

Language: C#

What operating system (Linux, Windows,...) and version?

Windows 11 Enterprise (10.0.22631 Build 22631)

What runtime / compiler are you using (e.g. .NET Core SDK version dotnet --info)

.NET 8.0.204

What did you do?

I followed the steps in the Microsoft tutorial for creating a .NET gRPC server and client. I modified the service operation to throw an exception with a long message as follows:

public class GreeterService : Greeter.GreeterBase
{
    public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromException<HelloReply>(new Exception(string.Join("", Enumerable.Repeat("soi-", 4100))));
    }
}

I have set MaxSendMessageSize to 10 MB on the server (MaxSendMessageSize = 10_485_760) and also set MaxReceiveMessageSize to 10 MB on the client. This is much larger than the length of the exception message.

If I make the operation return a long response message as in Task.FromResult(new HelloReply { Message = string.Join("", Enumerable.Repeat("soi-", 40000)) }) this works fine. This shows that the exception message isn't exceeding the maximum allowed message size.

What did you expect to see?

The following exception at the client:

Grpc.Core.RpcException: 'Status(StatusCode="Unknown", Detail="Exception was thrown by handler. Exception: soi-soi-soi-soi-soi-soi-soi-soi-soi-soi-soi-soi-soi-soi-soi-...

What did you see instead?

The following exception at the client:

Grpc.Core.RpcException: 'Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1). (HttpProtocolError) HttpProtocolException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1). (HttpProtocolError)", DebugException="System.Net.Http.HttpRequestException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1). (HttpProtocolError)")'

There is a maximum limit on response header size. When it is exceeded the request fails with a protocol error, which is what the client sees.

@JamesNK Does this mean that the exception message gets encoded into the response header as opposed to the response body?

@JamesNK Thanks. In that case fixing the issue is probably not viable.