grpc/grpc-dotnet

System.IO.InvalidDataException: Server stream prematurely closes on slow connection

coldays opened this issue · 1 comments

What version of gRPC and what language are you using?

gRPC 2.47.0, C#

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

Windows 11 (10.0.22621)

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

6.0.317

I have a simple download file api call that is server streaming:

service FileService {
    rpc GetFile(FileRequest) returns (streaming GetFileResult);
}
message FileRequest {
    string file_id = 1;
}
message GetFileResult{
    bool success = 1;
    string error_message = 2;
    bytes content = 3;
}

While on the same network everything works fine. When the client has to go through some hops (and/or through vpn) the call sometimes fail on the client side with this exception:

1. One or more errors occurred. (Status(StatusCode="Internal", Detail="Error reading next message. InvalidDataException: Unexpected end of content while reading the message content.", DebugException="System.IO.InvalidDataException: Unexpected end of content while reading the message content.")) 
2. Status(StatusCode="Internal", Detail="Error reading next message. InvalidDataException: Unexpected end of content while reading the message content.", DebugException="System.IO.InvalidDataException: Unexpected end of content while reading the message content.") 
3. Unexpected end of content while reading the message content. 

This can however be fixed by adding a delay to the end of the call on the server side like so:

public override async Task GetFile(...)
{
    while (/* stream not at EOF */)
        await responseStream.WriteAsync(response);
    await Task.Delay(100);
}

Seems when the server has written it's last message and goes out of scope of the call method, it breaks the connection with the client or disposes the outbound messages.