Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 4ad658d

Browse files
[Backport 5.2] Fix data access race condition in gRPC retry logic (#59488)
Fix data access race condition in gRPC retry logic (#59487) (cherry picked from commit adb95ea) Co-authored-by: Erik Seliger <[email protected]>
1 parent e464b48 commit 4ad658d

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

internal/grpc/retry/options.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ type CallOption struct {
116116
applyFunc func(opt *options)
117117
}
118118

119-
func reuseOrNewWithCallOptions(opt *options, callOptions []CallOption) *options {
120-
if len(callOptions) == 0 {
121-
return opt
122-
}
119+
func newWithCallOptions(opt *options, callOptions []CallOption) *options {
123120
optCopy := &options{}
124121
*optCopy = *opt
125122
for _, f := range callOptions {

internal/grpc/retry/retry.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ const (
3838
// The default configuration of the interceptor is to not retry *at all*. This behaviour can be
3939
// changed through options (e.g. WithMax) on creation of the interceptor or on call (through grpc.CallOptions).
4040
func UnaryClientInterceptor(logger log.Logger, optFuncs ...CallOption) grpc.UnaryClientInterceptor {
41-
intOpts := reuseOrNewWithCallOptions(defaultOptions, optFuncs)
41+
intOpts := newWithCallOptions(defaultOptions, optFuncs)
4242
return func(parentCtx context.Context, fullMethod string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
4343
tr := trace.FromContext(parentCtx)
4444
tr.SetAttributes(attribute.Bool(retriedTraceAttributeKey, false))
4545

4646
service, method := grpcutil.SplitMethodName(fullMethod)
4747

4848
grpcOpts, retryOpts := filterCallOptions(opts)
49-
callOpts := reuseOrNewWithCallOptions(intOpts, retryOpts)
49+
callOpts := newWithCallOptions(intOpts, retryOpts)
5050

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

112112
service, method := grpcutil.SplitMethodName(fullMethod)
113113

114114
grpcOpts, retryOpts := filterCallOptions(opts)
115-
callOpts := reuseOrNewWithCallOptions(intOpts, retryOpts)
115+
callOpts := newWithCallOptions(intOpts, retryOpts)
116116
// short circuit for simplicity, and avoiding allocations.
117117

118118
doTrace := makeTracingCallback(parentCtx, logger, service, method)

0 commit comments

Comments
 (0)