Skip to content
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

fix panic, when service Handler returns (nil, nil) #7227

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"io"
"math"
"reflect"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -641,7 +642,8 @@ func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byt
// error if it is too large to be transmitted by grpc. If msg is nil, it
// generates an empty message.
func encode(c baseCodec, msg any) ([]byte, error) {
if msg == nil { // NOTE: typed nils will not be caught by this check
if msg == nil ||
reflect.ValueOf(msg).IsNil() { // in gRPC, msg must be a ptr(or nil ptr)
return nil, nil
}
b, err := c.Marshal(msg)
Expand Down
18 changes: 18 additions & 0 deletions rpc_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ func (s) TestToRPCErr(t *testing.T) {
}
}

func (s) TestNilReply(t *testing.T) {
type XxxReply struct {
proto.Message
}

handler := func() (any, error) {
return func() (*XxxReply, error) {
return nil, nil
}()
}

reply, _ := handler()
raw, err := encode(encoding.GetCodec(protoenc.Name), reply)
if err != nil || raw != nil {
t.Fatal(err)
}
}

// bmEncode benchmarks encoding a Protocol Buffer message containing mSize
// bytes.
func bmEncode(b *testing.B, mSize int) {
Expand Down