Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong StatusCode when service is unavailable and Grpc.Net.Client.GrpcChannel is used #1076

Closed
kedrzu91 opened this issue Oct 13, 2020 · 2 comments · Fixed by #1092
Closed
Labels
bug Something isn't working
Milestone

Comments

@kedrzu91
Copy link

kedrzu91 commented Oct 13, 2020

What version of gRPC and what language are you using?

2.32.0

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

Windows 10

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

.NET Core SDK: Version: 3.1.402

What did you do?

I'm working on error handling. I want to detect when service is unavailable.
I've noticed that when I'm using Grpc.Core.Channel everything is ok - status code of RpcException is equal to Unavailable (correct).
But when I'm using Grpc.Net.Client.GrpcChannel status code of RpcException is equal to Internal (incorrect).

Below test that presents the issue:

[Test]
public void Wrong_status_code()
{
    var grpcCLibChannel = new Channel("localhost", port, ChannelCredentials.Insecure);
    var grpcCLibClient = new TestService.TestServiceClient(grpcCLibChannel);

    var exceptionCLib = Assert.ThrowsAsync<RpcException>(
        async () => await grpcCLibClient.TestMethodAsync(new TestModelInput()));
            
    TestContext.WriteLine(exceptionCLib);

    Assert.That(exceptionCLib.Status.StatusCode, Is.EqualTo(StatusCode.Unavailable));

    var uri = new UriBuilder("http", "localhost", port).Uri;
    var options = new GrpcChannelOptions() { Credentials = ChannelCredentials.Insecure };
    var grpcCSharpHttpClientChannel = GrpcChannel.ForAddress(uri, options);
    var grpcCSharpHttpClientClient = new TestService.TestServiceClient(grpcCSharpHttpClientChannel);

    var exceptionCSharpHttpClient = Assert.ThrowsAsync<RpcException>(
        async () => await grpcCSharpHttpClientClient.TestMethodAsync(new TestModelInput()));

    TestContext.WriteLine(exceptionCSharpHttpClient);
    Assert.That(exceptionCSharpHttpClient.Status.StatusCode, 
        Is.EqualTo(StatusCode.Internal)); // <--- !!!! should be Unavailable
}

The same issue exists when client is created by DI in ASP.NET Core (registered with extension: service.AddGrpcClient<T>)

I can detect that connection is unavailable based on exceptionCSharpHttpClient .Status.DebugException (which has HttpRequestException type) but the docs say that DebugException shouldn't be used in logic.

What did you expect to see?

RpcException should have status equal to Unavailable.

What did you see instead?

RpcException had status equal to Internal when Grpc.Net.Client.GrpcChannel was used.

@kedrzu91 kedrzu91 added the bug Something isn't working label Oct 13, 2020
@JamesNK
Copy link
Member

JamesNK commented Oct 14, 2020

What is the cause of the error? Is the server not running at the specified localhost + port.

@kedrzu91
Copy link
Author

Yes, server is not running at the specified localhost + port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants