Search code examples
protocol-buffersgrpcgrpc-go

server error: rpc error: code = Unavailable desc = transport is closing" in gRPC


I have a grpc server and a client (in my blog project). when I run the server, It seems everything is ok, when I run the client, I face this error, and both server and client close.

rpc error: code = Unavailable desc = transport is closing

I think error is related to this piece of code:

func newPost(c proto_blog.BlogServiceClient) {
    fmt.Println("Starting to do a Unary RPC")
    req := &proto_blog.ReqNewPost{
        Title: "How can we make an gRPC server?",
        Content: "First You have to.....\nAt the end, you have to....",
        Author: "Arsham Ahora",
        Date: fmt.Sprint(time.Now()),
    }

    res, err := c.NewPost(context.Background(), req)
    if err != nil {
        log.Fatalf("Error calling greet server: %v", err)
    }
    log.Printf("Response from Greet: %v", res.Id)
}

** I noticed this error is not related to whether you have Unary or Streaming.


Solution

  • I want to list some possible reasons cause code = Unavailable desc = transport is closing on gRPC per gRPC faq, in case of someone meets those reasons.


    This error means the connection the RPC is using was closed, and there are many possible reasons, including:

    • mis-configured transport credentials, connection failed on handshaking
    • bytes disrupted, possibly by a proxy in between
    • server shutdown
    • Keepalive parameters caused connection shutdown, for example if you have configured your server to terminate connections regularly to trigger DNS lookups. If this is the case, you may want to increase your MaxConnectionAgeGrace, to allow longer RPC calls to finish.

    It can be tricky to debug this because the error happens on the client side but the root cause of the connection being closed is on the server side. Turn on logging on both client and server, and see if there are any transport errors.

    The default logger is controlled by environment variables. Turn everything on like this:

    $ export GRPC_GO_LOG_VERBOSITY_LEVEL=99
    $ export GRPC_GO_LOG_SEVERITY_LEVEL=info