Skip to content

grpc: Fix cardinality violations in client streaming RPCs #8278

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,10 @@
if statusErr := a.transportStream.Status().Err(); statusErr != nil {
return statusErr
}
// received no msg and status ok for non-server streaming rpcs.
if !cs.desc.ServerStreams {
return status.Errorf(codes.Internal, "cardinality violation: received no response message from non-streaming RPC")
}
return io.EOF // indicates successful end of stream.
}

Expand Down Expand Up @@ -1478,6 +1482,10 @@
if statusErr := as.transportStream.Status().Err(); statusErr != nil {
return statusErr
}
// received no msg and status ok for non-server streaming rpcs.
if !as.desc.ServerStreams {
return status.Errorf(codes.Internal, "cardinality violation: received no response message from non-streaming RPC")
}

Check warning on line 1488 in stream.go

View check run for this annotation

Codecov / codecov/patch

stream.go#L1487-L1488

Added lines #L1487 - L1488 were not covered by tests
return io.EOF // indicates successful end of stream.
}
return toRPCErr(err)
Expand Down
119 changes: 116 additions & 3 deletions test/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3589,9 +3589,6 @@ func testClientStreamingError(t *testing.T, e env) {
// Tests that a client receives a cardinality violation error for client-streaming
// RPCs if the server doesn't send a message before returning status OK.
func (s) TestClientStreamingCardinalityViolation_ServerHandlerMissingSendAndClose(t *testing.T) {
// TODO : https://github.com/grpc/grpc-go/issues/8119 - remove `t.Skip()`
// after this is fixed.
t.Skip()
ss := &stubserver.StubServer{
StreamingInputCallF: func(_ testgrpc.TestService_StreamingInputCallServer) error {
// Returning status OK without sending a response message.This is a
Expand Down Expand Up @@ -3740,6 +3737,122 @@ func (s) TestClientStreaming_ReturnErrorAfterSendAndClose(t *testing.T) {
}
}

// Tests that a client receives a cardinality violation error for unary
// RPCs if the server doesn't send a message before returning status OK.
func (s) TestUnaryRPC_ServerSendsOnlyTrailersWithOK(t *testing.T) {
lis, err := testutils.LocalTCPListener()
if err != nil {
t.Fatal(err)
}
defer lis.Close()

s := grpc.NewServer()
serviceDesc := grpc.ServiceDesc{
ServiceName: "grpc.testing.TestService",
HandlerType: (*any)(nil),
Methods: []grpc.MethodDesc{},
Streams: []grpc.StreamDesc{
{
StreamName: "EmptyCall",
Handler: func(any, grpc.ServerStream) error {
return nil
},
ClientStreams: false,
ServerStreams: false,
},
},
Metadata: "grpc/testing/test.proto",
}
s.RegisterService(&serviceDesc, &testServer{})
go s.Serve(lis)
defer s.Stop()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
cc, err := grpc.NewClient(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("grpc.NewClient(%q) failed unexpectedly: %v", lis.Addr(), err)
}
defer cc.Close()

client := testgrpc.NewTestServiceClient(cc)
if _, err = client.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.Internal {
t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
}
}

// Tests that client will receive cardinality violation in subsequent calls to RecvMsg().
func (s) TestUnaryRPC_ClientCallRecvMsgTwice(t *testing.T) {
e := tcpTLSEnv
te := newTest(t, e)
defer te.tearDown()

te.startServer(&testServer{security: e.security})

cc := te.clientConn()
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

desc := &grpc.StreamDesc{
StreamName: "UnaryCall",
ServerStreams: false,
ClientStreams: false,
}
stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/UnaryCall")
if err != nil {
t.Fatalf("cc.NewStream() failed unexpectedly: %v", err)
}

if err := stream.SendMsg(&testpb.SimpleRequest{}); err != nil {
t.Fatalf("stream.SendMsg(_) = %v, want <nil>", err)
}

resp := &testpb.SimpleResponse{}
if err := stream.RecvMsg(resp); err != nil {
t.Fatalf("stream.RecvMsg() = %v , want <nil>", err)
}

if err = stream.RecvMsg(resp); status.Code(err) != codes.Internal {
t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
}
}

// Tests that client will receive cardinality violation in the subsequent calls to RecvMsg().
func (s) TestClientStreaming_ClientCallRecvMsgTwice(t *testing.T) {
ss := stubserver.StubServer{
StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
if err := stream.SendAndClose(&testpb.StreamingInputCallResponse{}); err != nil {
t.Errorf("stream.SendAndClose(_) = %v, want <nil>", err)
}
return nil
},
}
if err := ss.Start(nil); err != nil {
t.Fatal("Error starting server:", err)
}
defer ss.Stop()

ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
stream, err := ss.Client.StreamingInputCall(ctx)
if err != nil {
t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err)
}
if err := stream.Send(&testpb.StreamingInputCallRequest{}); err != nil {
t.Fatalf("stream.Send(_) = %v, want <nil>", err)
}
if err := stream.CloseSend(); err != nil {
t.Fatalf("stream.CloseSend() = %v, want <nil>", err)
}
resp := new(testpb.StreamingInputCallResponse)
if err := stream.RecvMsg(resp); err != nil {
t.Fatalf("stream.RecvMsg() = %v , want <nil>", err)
}
if err = stream.RecvMsg(resp); status.Code(err) != codes.Internal {
t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal)
}
}

// Tests that a client receives a cardinality violation error for client-streaming
// RPCs if the server call SendMsg multiple times.
func (s) TestClientStreaming_ServerHandlerSendMsgAfterSendMsg(t *testing.T) {
Expand Down