-
Notifications
You must be signed in to change notification settings - Fork 4.6k
grpc: Fix cardinality violations in non-client streaming RPCs. #8385
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
Changes from 5 commits
298edba
4c3a18a
fd5d614
7e4206c
324566b
9fbf6b7
9b9cddf
63565f2
67549e7
6030b90
dff08b3
f4f1b61
83e8664
5acd9ab
8f4f39b
6de922d
5f6c715
990efe1
7f6d31f
41d8328
59ad122
0f5248a
1ff8868
68fd0f8
19e9e71
0d30b18
83fd598
efa8e5a
29f6657
183b1da
749a52c
1b1800d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1580,6 +1580,7 @@ type serverStream struct { | |
| s *transport.ServerStream | ||
| p *parser | ||
| codec baseCodec | ||
| desc *StreamDesc | ||
|
|
||
| compressorV0 Compressor | ||
| compressorV1 encoding.Compressor | ||
|
|
@@ -1774,6 +1775,9 @@ func (ss *serverStream) RecvMsg(m any) (err error) { | |
| binlog.Log(ss.ctx, chc) | ||
| } | ||
| } | ||
| if !ss.desc.ClientStreams { | ||
|
||
| return status.Errorf(codes.Internal, "RecvMsg is called twice") | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return err | ||
| } | ||
| if err == io.ErrUnexpectedEOF { | ||
|
|
||
arjan-bal marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3740,6 +3740,232 @@ func (s) TestClientStreaming_ReturnErrorAfterSendAndClose(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // Tests the behavior for server-side streaming when server call | ||
| // RecvMsg twice. Second call to RecvMsg should fail with Internal | ||
| // error. | ||
| func (s) TestServerStreaming_ServerCallRecvMsgTwice(t *testing.T) { | ||
| lis, err := testutils.LocalTCPListener() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer lis.Close() | ||
|
|
||
| s := grpc.NewServer() | ||
|
||
| serviceDesc := grpc.ServiceDesc{ | ||
Pranjali-2501 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ServiceName: "grpc.testing.TestService", | ||
| HandlerType: (*any)(nil), | ||
| Methods: []grpc.MethodDesc{}, | ||
| Streams: []grpc.StreamDesc{ | ||
| { | ||
| StreamName: "FullDuplexCall", | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Handler: func(srv interface{}, stream grpc.ServerStream) error { | ||
| err := stream.RecvMsg(&testpb.Empty{}) | ||
| if err != nil { | ||
| t.Errorf("stream.RecvMsg() = %v, want <nil>", err) | ||
| } | ||
|
|
||
| if err = stream.RecvMsg(&testpb.Empty{}); status.Code(err) != codes.Internal { | ||
| t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal) | ||
| } | ||
| return nil | ||
| }, | ||
| ClientStreams: false, | ||
| ServerStreams: true, | ||
| }, | ||
| }, | ||
| Metadata: "grpc/testing/test.proto", | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| 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() | ||
|
|
||
| desc := &grpc.StreamDesc{ | ||
| StreamName: "FullDuplexCall", | ||
| ServerStreams: true, | ||
| ClientStreams: false, // This is the test case: client is *not* allowed to stream | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/FullDuplexCall") | ||
| if err != nil { | ||
| t.Fatalf("cc.NewStream() failed unexpectedly: %v", err) | ||
| } | ||
|
|
||
| if err := stream.SendMsg(&testpb.Empty{}); err != nil { | ||
| t.Errorf("stream.SendMsg() = %v, want <nil>", err) | ||
| } | ||
|
|
||
| if err := stream.RecvMsg(&testpb.Empty{}); status.Code(err) != codes.Internal { | ||
| t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal) | ||
| } | ||
| } | ||
|
|
||
| // Tests the behavior for server-side streaming when client call | ||
| // SendMsg twice. Second call to SendMsg should fail with Internal | ||
| // error. | ||
| func (s) TestServerStreaming_ClientCallSendMsgTwice(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() | ||
|
|
||
| desc := &grpc.StreamDesc{ | ||
| StreamName: "FullDuplexCall", | ||
| ServerStreams: true, | ||
| ClientStreams: false, | ||
| } | ||
|
|
||
| stream, err := cc.NewStream(ctx, desc, "/grpc.testing.TestService/FullDuplexCall") | ||
| if err != nil { | ||
| t.Fatalf("cc.NewStream() failed unexpectedly: %v", err) | ||
| } | ||
|
|
||
| if err := stream.SendMsg(&testpb.Empty{}); err != nil { | ||
| t.Errorf("stream.SendMsg() = %v, want <nil>", err) | ||
| } | ||
|
|
||
| if err := stream.SendMsg(&testpb.Empty{}); status.Code(err) != codes.Internal { | ||
| t.Errorf("stream.SendMsg() = %v, want error %v", status.Code(err), codes.Internal) | ||
| } | ||
| } | ||
|
|
||
| // Tests the behavior for unary RPC when server call | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // RecvMsg twice. Second call to RecvMsg should fail with Internal | ||
| // error. | ||
| func (s) TestUnaryRPC_ServerCallRecvMsgTwice(t *testing.T) { | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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: "UnaryCall", | ||
| Handler: func(srv interface{}, stream grpc.ServerStream) error { | ||
| err := stream.RecvMsg(&testpb.Empty{}) | ||
| if err != nil { | ||
| t.Errorf("stream.RecvMsg() = %v, want <nil>", err) | ||
| } | ||
|
|
||
| if err = stream.RecvMsg(&testpb.Empty{}); status.Code(err) != codes.Internal { | ||
| t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal) | ||
| } | ||
| 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() | ||
|
|
||
| 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.Empty{}); err != nil { | ||
| t.Errorf("stream.SendMsg() = %v, want <nil>", err) | ||
| } | ||
|
|
||
| if err := stream.RecvMsg(&testpb.Empty{}); status.Code(err) != codes.Internal { | ||
| t.Errorf("stream.RecvMsg() = %v, want error %v", status.Code(err), codes.Internal) | ||
| } | ||
| } | ||
|
|
||
| // Tests the behavior for unary RPC when client call | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // SendMsg twice. Second call to SendMsg should fail with Internal | ||
| // error. | ||
| func (s) TestUnaryRPC_ClientCallSendMsgTwice(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 | ||
| }) | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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() | ||
|
|
||
| desc := &grpc.StreamDesc{ | ||
| StreamName: "UnaryCall", | ||
| ServerStreams: false, | ||
| ClientStreams: false, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like we should have a test case that sets this (And if we don't have such a check we may want to add one.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to clarify — you're suggesting me to add a test where the client behaves as client/bidi-streaming and sends zero request, while server behave as server-streaming, and then assert that it fails on the server side due to a cardinality violation. Is that correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. This exact test, pretty much, but set this field to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have modified the test to a table-driven test, where server will run against multiple streamdesc including client-streaming, server-streaming and bidi-streaming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The key difference between the two cases that I'd like to see is whether the client knows it's required to send a message. In the case where the client knows ( |
||
| } | ||
|
|
||
| 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.Empty{}); err != nil { | ||
| t.Errorf("stream.SendMsg() = %v, want <nil>", err) | ||
| } | ||
|
|
||
| if err := stream.SendMsg(&testpb.Empty{}); status.Code(err) != codes.Internal { | ||
| t.Errorf("stream.SendMsg() = %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) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.