grpc / grpc-go

The Go language implementation of gRPC. HTTP/2 based RPC

Home Page:https://grpc.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question: Bidirectional Streaming get error: rpc error: code = `Canceled` desc = `context canceled`

afifurrohman-id opened this issue · comments

Description

I tried bidirectional streaming in example get expected result, but when i tried from scratch i get the error above, i cannot reproduce with existing example, so i want to ask, from where this error come, it's not clear for me.
Even when i tried enable debug log still didn't get it.

I hope this not environment problem.

Environment

  • uname -a
Linux laptop 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 GNU/Linux
  • go version
go version go1.22.0 linux/amd64
  • protoc --version
libprotoc 26.0-rc2
  • protoc-gen-go --version
protoc-gen-go v1.32.0
  • protoc-gen-go-grpc --version
protoc-gen-go-grpc 1.3.0

For go-grpc lib version expand my module file in bottom.

Sources Code

  • cmd/server/main.go
 package main

import (
        "net"
        "io"
        "example.com/grpc/model"
        "google.golang.org/grpc"
        "google.golang.org/grpc/credentials/insecure"
)

type Server struct {
        model.UnimplementedEmptyServer
}

func (srv *Server) Stream(stream model.Empty_StreamServer) error {
        for {
                req, err := stream.Recv()
                if err == io.EOF {
                        return nil
                }
                if err != nil {
                        panic(err)
                }
                if err := stream.Send(req); err != nil {
                        panic(err)
                }
        }

}

func main() {
        opt := grpc.Creds(insecure.NewCredentials())

        server := grpc.NewServer(opt)

        listen, err := net.Listen("tcp", "localhost:50051")
        if err != nil {
                panic(err)
        }

        model.RegisterEmptyServer(server, new(Server))
        server.Serve(listen)

}
  • cmd/client/main.go
package main

import (
        "context"
        "io"
        "fmt"
        "example.com/grpc/model"
        "google.golang.org/grpc"
        "google.golang.org/grpc/credentials/insecure"
        "google.golang.org/protobuf/types/known/emptypb"
)

type Config struct {
        ctx context.Context
        client model.EmptyClient
        wait chan struct{}
}

func DoStream(cfg *Config) {
        stream, err := cfg.client.Stream(cfg.ctx)
        if err != nil {
                panic(err)
        }
        defer stream.CloseSend()

        const max = 10

        for i := 1; i <= max; i++ {
                if err := stream.Send(new(emptypb.Empty)); err != nil {
                        panic(err)
                }

                res, err := stream.Recv()
                if err == io.EOF {
                        break
                }
                if err != nil {
                        panic(err)
                }
                fmt.Println("stream create recv: ", res)

        }

        cfg.wait <- struct{}{}
}

func main() {
        creds := insecure.NewCredentials()

        conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(creds))
        if err != nil {
                panic(err)
        }
        defer conn.Close()

        client := model.NewEmptyClient(conn)
        ctx := context.Background()
        const max = 30
        wait := make(chan struct{}, max)
        defer close(wait)

        for range max {
                go DoStream(&Config{ctx,client,wait})
        }

        for range max {
                <-wait
        }
}
  • model/service.proto
syntax = "proto3";
package model;
import "google/protobuf/empty.proto";
option go_package = "example.com/grpc/model";

service Empty {
        rpc Stream(stream google.protobuf.Empty) returns (stream google.protobuf.Empty);
}

Run

  • Generate Code
protoc --proto_path model \
        --go_out=model --go_opt=paths=source_relative \
        --go-grpc_out=model --go-grpc_opt=paths=source_relative \
        model/*.proto
  • Run Server
GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info go run cmd/server/main.go
  • Run Client
GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info go run cmd/client/main.go
Generated Code
  • model/service.pb.go
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//      protoc-gen-go v1.32.0
//      protoc        v5.26.0--rc2
// source: service.proto

package model

import (
        protoreflect "google.golang.org/protobuf/reflect/protoreflect"
        protoimpl "google.golang.org/protobuf/runtime/protoimpl"
        emptypb "google.golang.org/protobuf/types/known/emptypb"
        reflect "reflect"
)

const (
        // Verify that this generated code is sufficiently up-to-date.
        _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
        // Verify that runtime/protoimpl is sufficiently up-to-date.
        _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)

var File_service_proto protoreflect.FileDescriptor

var file_service_proto_rawDesc = []byte{
        0x0a, 0x0d, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12,
        0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70,
        0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72,
        0x6f, 0x74, 0x6f, 0x32, 0x45, 0x0a, 0x05, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x3c, 0x0a, 0x06,
        0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
        0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x16,
        0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
        0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x28, 0x01, 0x30, 0x01, 0x42, 0x18, 0x5a, 0x16, 0x65, 0x78,
        0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x6d,
        0x6f, 0x64, 0x65, 0x6c, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var file_service_proto_goTypes = []interface{}{
        (*emptypb.Empty)(nil), // 0: google.protobuf.Empty
}
var file_service_proto_depIdxs = []int32{
        0, // 0: model.Empty.Stream:input_type -> google.protobuf.Empty
        0, // 1: model.Empty.Stream:output_type -> google.protobuf.Empty
        1, // [1:2] is the sub-list for method output_type
        0, // [0:1] is the sub-list for method input_type
        0, // [0:0] is the sub-list for extension type_name
        0, // [0:0] is the sub-list for extension extendee
        0, // [0:0] is the sub-list for field type_name
}

func init() { file_service_proto_init() }
func file_service_proto_init() {
        if File_service_proto != nil {
                return
        }
        type x struct{}
        out := protoimpl.TypeBuilder{
                File: protoimpl.DescBuilder{
                        GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
                        RawDescriptor: file_service_proto_rawDesc,
                        NumEnums:      0,
                        NumMessages:   0,
                        NumExtensions: 0,
                        NumServices:   1,
                },
                GoTypes:           file_service_proto_goTypes,
                DependencyIndexes: file_service_proto_depIdxs,
        }.Build()
        File_service_proto = out.File
        file_service_proto_rawDesc = nil
        file_service_proto_goTypes = nil
        file_service_proto_depIdxs = nil
}
  • model/service_grpc.pb.go
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc             v5.26.0--rc2
// source: service.proto

package model

import (
        context "context"
        grpc "google.golang.org/grpc"
        codes "google.golang.org/grpc/codes"
        status "google.golang.org/grpc/status"
        emptypb "google.golang.org/protobuf/types/known/emptypb"
)

// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7

const (
        Empty_Stream_FullMethodName = "/model.Empty/Stream"
)

// EmptyClient is the client API for Empty service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type EmptyClient interface {
        Stream(ctx context.Context, opts ...grpc.CallOption) (Empty_StreamClient, error)
}

type emptyClient struct {
        cc grpc.ClientConnInterface
}

func NewEmptyClient(cc grpc.ClientConnInterface) EmptyClient {
        return &emptyClient{cc}
}

func (c *emptyClient) Stream(ctx context.Context, opts ...grpc.CallOption) (Empty_StreamClient, error) {
        stream, err := c.cc.NewStream(ctx, &Empty_ServiceDesc.Streams[0], Empty_Stream_FullMethodName, opts...)
        if err != nil {
                return nil, err
        }
        x := &emptyStreamClient{stream}
        return x, nil
}

type Empty_StreamClient interface {
        Send(*emptypb.Empty) error
        Recv() (*emptypb.Empty, error)
        grpc.ClientStream
}

type emptyStreamClient struct {
        grpc.ClientStream
}

func (x *emptyStreamClient) Send(m *emptypb.Empty) error {
        return x.ClientStream.SendMsg(m)
}

func (x *emptyStreamClient) Recv() (*emptypb.Empty, error) {
        m := new(emptypb.Empty)
        if err := x.ClientStream.RecvMsg(m); err != nil {
                return nil, err
        }
        return m, nil
}

// EmptyServer is the server API for Empty service.
// All implementations must embed UnimplementedEmptyServer
// for forward compatibility
type EmptyServer interface {
        Stream(Empty_StreamServer) error
        mustEmbedUnimplementedEmptyServer()
}

// UnimplementedEmptyServer must be embedded to have forward compatible implementations.
type UnimplementedEmptyServer struct {
}

func (UnimplementedEmptyServer) Stream(Empty_StreamServer) error {
        return status.Errorf(codes.Unimplemented, "method Stream not implemented")
}
func (UnimplementedEmptyServer) mustEmbedUnimplementedEmptyServer() {}

// UnsafeEmptyServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to EmptyServer will
// result in compilation errors.
type UnsafeEmptyServer interface {
        mustEmbedUnimplementedEmptyServer()
}

func RegisterEmptyServer(s grpc.ServiceRegistrar, srv EmptyServer) {
        s.RegisterService(&Empty_ServiceDesc, srv)
}

func _Empty_Stream_Handler(srv interface{}, stream grpc.ServerStream) error {
        return srv.(EmptyServer).Stream(&emptyStreamServer{stream})
}

type Empty_StreamServer interface {
        Send(*emptypb.Empty) error
        Recv() (*emptypb.Empty, error)
        grpc.ServerStream
}

type emptyStreamServer struct {
        grpc.ServerStream
}

func (x *emptyStreamServer) Send(m *emptypb.Empty) error {
        return x.ServerStream.SendMsg(m)
}

func (x *emptyStreamServer) Recv() (*emptypb.Empty, error) {
        m := new(emptypb.Empty)
        if err := x.ServerStream.RecvMsg(m); err != nil {
                return nil, err
        }
        return m, nil
}

// Empty_ServiceDesc is the grpc.ServiceDesc for Empty service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var Empty_ServiceDesc = grpc.ServiceDesc{
        ServiceName: "model.Empty",
        HandlerType: (*EmptyServer)(nil),
        Methods:     []grpc.MethodDesc{},
        Streams: []grpc.StreamDesc{
                {
                        StreamName:    "Stream",
                        Handler:       _Empty_Stream_Handler,
                        ServerStreams: true,
                        ClientStreams: true,
                },
        },
        Metadata: "service.proto",
}
Module file
  • go.mod
module example.com/grpc

go 1.22

require (
        google.golang.org/grpc v1.63.2
        google.golang.org/protobuf v1.34.0
)

require (
        golang.org/x/net v0.24.0 // indirect
        golang.org/x/sys v0.19.0 // indirect
        golang.org/x/text v0.14.0 // indirect
        google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
)
  • go.sum
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 h1:DujSIu+2tC9Ht0aPNA7jgj23Iq8Ewi5sgkQ++wdvonE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

I can confirm this is not environment issue.
I have tried same tools version with Project IDX but still have those error.

Even with newer each tool version is still get same error.

@afifurrohman-id I will take a look

@afifurrohman-id in your server code "DoStream" function, it looks like you are panicking instead of returning error

func DoStream(cfg *Config) {
        stream, err := cfg.client.Stream(cfg.ctx)
        if err != nil {
                panic(err)
        }
        defer stream.CloseSend()

        const max = 10

        for i := 1; i <= max; i++ {
                if err := stream.Send(new(emptypb.Empty)); err != nil {
                        panic(err)
                }

                res, err := stream.Recv()
                if err == io.EOF {
                        break
                }
                if err != nil {
                        panic(err)
                }
                fmt.Println("stream create recv: ", res)

        }

        cfg.wait <- struct{}{}
}

It's generally recommended to avoid using panic for error handling and instead propagate errors up the call stack using return values instead of panicking. Panics can indirectly affect context cancellation by terminating goroutines and their associated cleanup logic.

Can you try returning the errors from server (refer RouteChat example) and log at client side. Also, share both server and client logs

Can you try returning the errors from server (refer RouteChat example) and log at client side. Also, share both server and client logs

I have try reproduced and modify those example with if err panic and it's seem still work.
But instead, like in my issue description it panic when i try start from scratch.

Here my client log:

GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info go run cmd/client/main.go
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel created
2024/05/05 10:51:27 INFO: [core] [Channel #1]original dial target is: "localhost:50051"
2024/05/05 10:51:27 INFO: [core] [Channel #1]parsed dial target is: resolver.Target{URL:url.URL{Scheme:"localhost", Opaque:"50051", User:(*url.Userinfo)(nil), Host:"", Path:"", RawPath:"", OmitHost:false, ForceQuery:false, RawQuery:"", Fragment:"", RawFragment:""}}
2024/05/05 10:51:27 INFO: [core] [Channel #1]fallback to scheme "dns"
2024/05/05 10:51:27 INFO: [core] [Channel #1]parsed dial target is: dns:///localhost:50051
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel authority set to "localhost:50051"
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel exiting idle mode
2024/05/05 10:51:27 INFO: [core] [Channel #1]Resolver state updated: {
  "Addresses": [
    {
      "Addr": "127.0.0.1:50051",
      "ServerName": "",
      "Attributes": null,
      "BalancerAttributes": null,
      "Metadata": null
    },
    {
      "Addr": "[::1]:50051",
      "ServerName": "",
      "Attributes": null,
      "BalancerAttributes": null,
      "Metadata": null
    }
  ],
  "Endpoints": [
    {
      "Addresses": [
        {
          "Addr": "127.0.0.1:50051",
          "ServerName": "",
          "Attributes": null,
          "BalancerAttributes": null,
          "Metadata": null
        }
      ],
      "Attributes": null
    },
    {
      "Addresses": [
        {
          "Addr": "[::1]:50051",
          "ServerName": "",
          "Attributes": null,
          "BalancerAttributes": null,
          "Metadata": null
        }
      ],
      "Attributes": null
    }
  ],
  "ServiceConfig": null,
  "Attributes": null
} (resolver returned new addresses)
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel switches to new LB policy "pick_first"
2024/05/05 10:51:27 INFO: [core] [pick-first-lb 0xc000094cf0] Received new config {
  "shuffleAddressList": false
}, resolver state {
  "Addresses": [
    {
      "Addr": "127.0.0.1:50051",
      "ServerName": "",
      "Attributes": null,
      "BalancerAttributes": null,
      "Metadata": null
    },
    {
      "Addr": "[::1]:50051",
      "ServerName": "",
      "Attributes": null,
      "BalancerAttributes": null,
      "Metadata": null
    }
  ],
  "Endpoints": [
    {
      "Addresses": [
        {
          "Addr": "127.0.0.1:50051",
          "ServerName": "",
          "Attributes": null,
          "BalancerAttributes": null,
          "Metadata": null
        }
      ],
      "Attributes": null
    },
    {
      "Addresses": [
        {
          "Addr": "[::1]:50051",
          "ServerName": "",
          "Attributes": null,
          "BalancerAttributes": null,
          "Metadata": null
        }
      ],
      "Attributes": null
    }
  ],
  "ServiceConfig": null,
  "Attributes": null
}
2024/05/05 10:51:27 INFO: [core] [Channel #1 SubChannel #2]Subchannel created
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel Connectivity change to CONNECTING
2024/05/05 10:51:27 INFO: [core] [Channel #1 SubChannel #2]Subchannel Connectivity change to CONNECTING
2024/05/05 10:51:27 INFO: [core] [Channel #1 SubChannel #2]Subchannel picks a new address "127.0.0.1:50051" to connect
2024/05/05 10:51:27 INFO: [core] [pick-first-lb 0xc000094cf0] Received SubConn state update: 0xc000094d80, {ConnectivityState:CONNECTING ConnectionError:<nil>}
2024/05/05 10:51:27 INFO: [core] [Channel #1 SubChannel #2]Subchannel Connectivity change to READY
2024/05/05 10:51:27 INFO: [core] [pick-first-lb 0xc000094cf0] Received SubConn state update: 0xc000094d80, {ConnectivityState:READY ConnectionError:<nil>}
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel Connectivity change to READY
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
stream create recv:  
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel Connectivity change to SHUTDOWN
2024/05/05 10:51:27 INFO: [core] [Channel #1]Closing the name resolver
2024/05/05 10:51:27 INFO: [core] [Channel #1]ccBalancerWrapper: closing
2024/05/05 10:51:27 INFO: [core] [Channel #1 SubChannel #2]Subchannel Connectivity change to SHUTDOWN
2024/05/05 10:51:27 INFO: [core] [Channel #1 SubChannel #2]Subchannel deleted
2024/05/05 10:51:27 INFO: [transport] [client-transport 0xc0000e6488] Closing: rpc error: code = Canceled desc = grpc: the client connection is closing
2024/05/05 10:51:27 INFO: [core] [Channel #1]Channel deleted

Here my server log:

GRPC_GO_LOG_VERBOSITY_LEVEL=99 GRPC_GO_LOG_SEVERITY_LEVEL=info go run cmd/server/main.go
2024/05/05 10:51:27 INFO: [core] [Server #1]Server created
2024/05/05 10:51:27 INFO: [core] [Server #1 ListenSocket #2]ListenSocket created
2024/05/05 10:51:27 INFO: [transport] [server-transport 0xc000002180] Closing: EOF
panic: rpc error: code = Canceled desc = context canceled

goroutine 50 [running]:
main.(*Server).Stream(0xc45e80?, {0x961258, 0xc0000255d0})
        /home/user/grpc/cmd/server/main.go:22 +0xb8
example.com/grpc/model._Empty_Stream_Handler({0x831420?, 0xcbdb40}, {0x95f138, 0xc0000b6b40})
        /home/user/grpc/model/service_grpc.pb.go:101 +0xd8
google.golang.org/grpc.(*Server).processStreamingRPC(0xc0001ac200, {0x95e438, 0xc000280000}, {0x9617d8, 0xc000002180}, 0xc0000d3e60, 0xc0001b8ea0, 0xc49d60, 0x0)
        /home/user/go/pkg/mod/google.golang.org/grpc@v1.63.2/server.go:1663 +0x1208
google.golang.org/grpc.(*Server).handleStream(0xc0001ac200, {0x9617d8, 0xc000002180}, 0xc0000d3e60)
        /home/user/go/pkg/mod/google.golang.org/grpc@v1.63.2/server.go:1784 +0xe3a
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        /home/user/go/pkg/mod/google.golang.org/grpc@v1.63.2/server.go:1019 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 22
        /home/user/go/pkg/mod/google.golang.org/grpc@v1.63.2/server.go:1030 +0x125
exit status 2

Thanks for sharing the logs @afifurrohman-id. Based on the client and server logs, the setup is working as intended.

Establishing Connection:
The logs indicate that the gRPC client successfully establishes a connection (Channel 1) with the server (127.0.0.1:50051). The channel transitions through the CONNECTING and READY states, which suggests that the client is able to communicate with the server.

Stream Communication:
The client initiates a streaming RPC (Stream) to the server and continuously receives responses (stream create recv:) from the server. This behavior aligns with the intended use of bidirectional streaming where the client sends messages and receives responses asynchronously.

Channel Cleanup on Completion:
The logs show the channel transitioning to the SHUTDOWN state after the client completes its communication with the server. This is expected behavior for gracefully closing the gRPC channel and associated resources once the client-side operations are finished (for range max finishes).

Client Connection Closure:
The closure of the client connection (client-transport) with a Canceled error message indicates that the client is closing the connection intentionally or in response to some application-specific condition (e.g., all expected messages received, end of communication).

These are some improvements you can do to ensure controlled concurrency, timeout handling, and reliable error management in your gRPC client

  • Use context.WithTimeout to create a context with a timeout for gRPC operations. This ensures that operations respect a maximum duration and handle timeouts gracefully.
  • Implement controlled concurrency using a worker pool or synchronization mechanisms (e.g., wait groups) to limit the number of concurrent goroutines executing DoStream. This prevents excessive goroutine creation and manages resource usage effectively.
var wg sync.WaitGroup
for i := 0; i < max; i++ {
    wg.Add(1)
    go func() {
        defer wg.Done()
        DoStream(&Config{ctx, client, wait})
    }()
}
wg.Wait() // Wait for all goroutines to complete
  • Replace panic(err) with explicit error handling using log or returning errors to the caller for proper error propagation and graceful termination of operations.

Closing the issue for now. Feel free to reopen if any more clarification is needed