Skip to content

Commit 6fecf28

Browse files
authored
Reopening: Server shouldn't Fatalf in case it fails to encode. (#1276)
* Server shouldn't Fatalf in case it fails to encode.
1 parent a8cd0c1 commit 6fecf28

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor,
119119
}
120120
outBuf, err := encode(dopts.codec, args, compressor, cbuf, outPayload)
121121
if err != nil {
122-
return Errorf(codes.Internal, "grpc: %v", err)
122+
return err
123123
}
124124
if c.maxSendMessageSize == nil {
125125
return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")

rpc_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func encode(c Codec, msg interface{}, cp Compressor, cbuf *bytes.Buffer, outPayl
314314
// TODO(zhaoq): optimize to reduce memory alloc and copying.
315315
b, err = c.Marshal(msg)
316316
if err != nil {
317-
return nil, err
317+
return nil, Errorf(codes.Internal, "grpc: error while marshaling: %v", err.Error())
318318
}
319319
if outPayload != nil {
320320
outPayload.Payload = msg
@@ -324,7 +324,7 @@ func encode(c Codec, msg interface{}, cp Compressor, cbuf *bytes.Buffer, outPayl
324324
}
325325
if cp != nil {
326326
if err := cp.Do(cbuf, b); err != nil {
327-
return nil, err
327+
return nil, Errorf(codes.Internal, "grpc: error while compressing: %v", err.Error())
328328
}
329329
b = cbuf.Bytes()
330330
}

server.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -664,14 +664,8 @@ func (s *Server) sendResponse(t transport.ServerTransport, stream *transport.Str
664664
}
665665
p, err := encode(s.opts.codec, msg, cp, cbuf, outPayload)
666666
if err != nil {
667-
// This typically indicates a fatal issue (e.g., memory
668-
// corruption or hardware faults) the application program
669-
// cannot handle.
670-
//
671-
// TODO(zhaoq): There exist other options also such as only closing the
672-
// faulty stream locally and remotely (Other streams can keep going). Find
673-
// the optimal option.
674-
grpclog.Fatalf("grpc: Server failed to encode response %v", err)
667+
grpclog.Println("grpc: server failed to encode response: ", err)
668+
return err
675669
}
676670
if len(p) > s.opts.maxSendMessageSize {
677671
return status.Errorf(codes.ResourceExhausted, "grpc: trying to send message larger than max (%d vs. %d)", len(p), s.opts.maxSendMessageSize)

stream.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (cs *clientStream) SendMsg(m interface{}) (err error) {
364364
}
365365
}()
366366
if err != nil {
367-
return Errorf(codes.Internal, "grpc: %v", err)
367+
return err
368368
}
369369
if cs.c.maxSendMessageSize == nil {
370370
return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)")
@@ -606,7 +606,6 @@ func (ss *serverStream) SendMsg(m interface{}) (err error) {
606606
}
607607
}()
608608
if err != nil {
609-
err = Errorf(codes.Internal, "grpc: %v", err)
610609
return err
611610
}
612611
if len(out) > ss.maxSendMessageSize {

test/end2end_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4795,3 +4795,46 @@ func testPerRPCCredentialsViaDialOptionsAndCallOptions(t *testing.T, e env) {
47954795
t.Fatalf("Test failed. Reason: %v", err)
47964796
}
47974797
}
4798+
4799+
type errCodec struct {
4800+
noError bool
4801+
}
4802+
4803+
func (c *errCodec) Marshal(v interface{}) ([]byte, error) {
4804+
if c.noError {
4805+
return []byte{}, nil
4806+
}
4807+
return nil, fmt.Errorf("3987^12 + 4365^12 = 4472^12")
4808+
}
4809+
4810+
func (c *errCodec) Unmarshal(data []byte, v interface{}) error {
4811+
return nil
4812+
}
4813+
4814+
func (c *errCodec) String() string {
4815+
return "Fermat's near-miss."
4816+
}
4817+
4818+
func TestEncodeDoesntPanic(t *testing.T) {
4819+
defer leakCheck(t)()
4820+
for _, e := range listTestEnv() {
4821+
testEncodeDoesntPanic(t, e)
4822+
}
4823+
}
4824+
4825+
func testEncodeDoesntPanic(t *testing.T, e env) {
4826+
te := newTest(t, e)
4827+
erc := &errCodec{}
4828+
te.customCodec = erc
4829+
te.startServer(&testServer{security: e.security})
4830+
defer te.tearDown()
4831+
te.customCodec = nil
4832+
tc := testpb.NewTestServiceClient(te.clientConn())
4833+
// Failure case, should not panic.
4834+
tc.EmptyCall(context.Background(), &testpb.Empty{})
4835+
erc.noError = true
4836+
// Passing case.
4837+
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
4838+
t.Fatalf("EmptyCall(_, _) = _, %v, want _, <nil>", err)
4839+
}
4840+
}

0 commit comments

Comments
 (0)