Skip to content
Merged
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
12 changes: 10 additions & 2 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.Error(codes.Internal, "cardinality violation: received no response message from non-streaming RPC")
}
return io.EOF // indicates successful end of stream.
}

Expand Down Expand Up @@ -1171,7 +1175,7 @@
} else if err != nil {
return toRPCErr(err)
}
return status.Errorf(codes.Internal, "cardinality violation: expected <EOF> for non server-streaming RPCs, but received another message")
return status.Error(codes.Internal, "cardinality violation: expected <EOF> for non server-streaming RPCs, but received another message")
}

func (a *csAttempt) finish(err error) {
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.Error(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 All @@ -1495,7 +1503,7 @@
} else if err != nil {
return toRPCErr(err)
}
return status.Errorf(codes.Internal, "cardinality violation: expected <EOF> for non server-streaming RPCs, but received another message")
return status.Error(codes.Internal, "cardinality violation: expected <EOF> for non server-streaming RPCs, but received another message")

Check warning on line 1506 in stream.go

View check run for this annotation

Codecov / codecov/patch

stream.go#L1506

Added line #L1506 was not covered by tests
}

func (as *addrConnStream) finish(err error) {
Expand Down
110 changes: 106 additions & 4 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,8 +3737,113 @@ 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()

ss := grpc.UnknownServiceHandler(func(any, grpc.ServerStream) error {
return nil
})

s := grpc.NewServer(ss)
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 violations when calling
// RecvMsg() multiple times for non-streaming response streams.
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 violations when calling
// RecvMsg() multiple times for non-streaming response streams.
func (s) TestClientStreaming_ClientCallRecvMsgTwice(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way is this different from the previous test case? The server side is different, but actually does the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both tests does the same thing, only difference is StreamDesc.ClientStreams is set to false in this test(Unary RPC) and set to true in previous test(Client Streaming RPC)

Copy link
Contributor

@arjan-bal arjan-bal Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is actually a fix for for "non-server streaming" RPCs instead of "client streaming RPCs". I've updated the PR title to reflect the same. So the value of StreamDesc.ClientStreams should not matter, but it's okay to have test coverage for this.

I noticed that none of the tests send 2 response messages from the server. This shows up as missing coverage reported by Codecov around line 1486. Can you please have the server send back two response messages here? You should use the UnknownServiceHandler for this to ensure the server doesn't skip sending the message after noticing it's a non-server streaming RPC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline: line 1486 is in addrConnStream, not csAttempt. I had initially thought the missing coverage was in csAttempt. addrConnStream is largely similar to csAttempt but is only used for establishing health streams. There is a plan to unify it with csAttempt in the future. Adding test coverage for addrConnStream may be difficult and is not necessary at this point.

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.
// RPCs if the server call SendMsg() multiple times.
func (s) TestClientStreaming_ServerHandlerSendMsgAfterSendMsg(t *testing.T) {
ss := stubserver.StubServer{
StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
Expand Down