Skip to content

Commit

Permalink
client: Fix race when using both client-side default CallOptions and …
Browse files Browse the repository at this point in the history
…per-call CallOptions (grpc#1948)
  • Loading branch information
jhump authored and dfawley committed Mar 29, 2018
1 parent f72b28a commit 1a70180
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion call.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@ import (
func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error {
// allow interceptor to see all applicable call options, which means those
// configured as defaults from dial option as well as per-call options
opts = append(cc.dopts.callOptions, opts...)
opts = combine(cc.dopts.callOptions, opts)

if cc.dopts.unaryInt != nil {
return cc.dopts.unaryInt(ctx, method, args, reply, cc, invoke, opts...)
}
return invoke(ctx, method, args, reply, cc, opts...)
}

func combine(o1 []CallOption, o2 []CallOption) []CallOption {
// we don't use append because o1 could have extra capacity whose
// elements would be overwritten, which could cause inadvertent
// sharing (and race connditions) between concurrent calls
if len(o1) == 0 {
return o2
} else if len(o2) == 0 {
return o1
}
ret := make([]CallOption, len(o1)+len(o2))
copy(ret, o1)
copy(ret[len(o1):], o2)
return ret
}

// Invoke sends the RPC request on the wire and returns after response is
// received. This is typically called by generated code.
//
Expand Down
2 changes: 1 addition & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type ClientStream interface {
func (cc *ClientConn) NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) {
// allow interceptor to see all applicable call options, which means those
// configured as defaults from dial option as well as per-call options
opts = append(cc.dopts.callOptions, opts...)
opts = combine(cc.dopts.callOptions, opts)

if cc.dopts.streamInt != nil {
return cc.dopts.streamInt(ctx, desc, cc, method, newClientStream, opts...)
Expand Down

0 comments on commit 1a70180

Please sign in to comment.