Skip to content

Commit

Permalink
[Backport 5.2] Fix data access race condition in gRPC retry logic (#5…
Browse files Browse the repository at this point in the history
…9488)

Fix data access race condition in gRPC retry logic (#59487)

(cherry picked from commit adb95ea)

Co-authored-by: Erik Seliger <erikseliger@me.com>
  • Loading branch information
sourcegraph-release-guild-bot and eseliger committed Jan 10, 2024
1 parent e464b48 commit 4ad658d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
5 changes: 1 addition & 4 deletions internal/grpc/retry/options.go
Expand Up @@ -116,10 +116,7 @@ type CallOption struct {
applyFunc func(opt *options)
}

func reuseOrNewWithCallOptions(opt *options, callOptions []CallOption) *options {
if len(callOptions) == 0 {
return opt
}
func newWithCallOptions(opt *options, callOptions []CallOption) *options {
optCopy := &options{}
*optCopy = *opt
for _, f := range callOptions {
Expand Down
8 changes: 4 additions & 4 deletions internal/grpc/retry/retry.go
Expand Up @@ -38,15 +38,15 @@ const (
// The default configuration of the interceptor is to not retry *at all*. This behaviour can be
// changed through options (e.g. WithMax) on creation of the interceptor or on call (through grpc.CallOptions).
func UnaryClientInterceptor(logger log.Logger, optFuncs ...CallOption) grpc.UnaryClientInterceptor {
intOpts := reuseOrNewWithCallOptions(defaultOptions, optFuncs)
intOpts := newWithCallOptions(defaultOptions, optFuncs)
return func(parentCtx context.Context, fullMethod string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
tr := trace.FromContext(parentCtx)
tr.SetAttributes(attribute.Bool(retriedTraceAttributeKey, false))

service, method := grpcutil.SplitMethodName(fullMethod)

grpcOpts, retryOpts := filterCallOptions(opts)
callOpts := reuseOrNewWithCallOptions(intOpts, retryOpts)
callOpts := newWithCallOptions(intOpts, retryOpts)

doTrace := makeTracingCallback(parentCtx, logger, service, method)
originalCallback := callOpts.onRetryCallback
Expand Down Expand Up @@ -104,15 +104,15 @@ func UnaryClientInterceptor(logger log.Logger, optFuncs ...CallOption) grpc.Unar
// to buffer the messages sent by the client. If retry is enabled on any other streams (ClientStreams,
// BidiStreams), the retry interceptor will fail the call.
func StreamClientInterceptor(logger log.Logger, optFuncs ...CallOption) grpc.StreamClientInterceptor {
intOpts := reuseOrNewWithCallOptions(defaultOptions, optFuncs)
intOpts := newWithCallOptions(defaultOptions, optFuncs)
return func(parentCtx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, fullMethod string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
tr := trace.FromContext(parentCtx)
tr.SetAttributes(attribute.Bool(retriedTraceAttributeKey, false))

service, method := grpcutil.SplitMethodName(fullMethod)

grpcOpts, retryOpts := filterCallOptions(opts)
callOpts := reuseOrNewWithCallOptions(intOpts, retryOpts)
callOpts := newWithCallOptions(intOpts, retryOpts)
// short circuit for simplicity, and avoiding allocations.

doTrace := makeTracingCallback(parentCtx, logger, service, method)
Expand Down

0 comments on commit 4ad658d

Please sign in to comment.