Skip to content

Commit

Permalink
Merge pull request #11706 from gyuho/fix-race
Browse files Browse the repository at this point in the history
clientv3: fix racy writes to context key
  • Loading branch information
spzala authored Mar 19, 2020
2 parents bf883bd + b6d9871 commit 45a45d3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions .words
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mutex
prefetching
protobuf
prometheus
racey
rafthttp
repin
rpc
Expand Down
10 changes: 6 additions & 4 deletions clientv3/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ func WithRequireLeader(ctx context.Context) context.Context {
md = metadata.Pairs(rpctypes.MetadataRequireLeaderKey, rpctypes.MetadataHasLeader)
return metadata.NewOutgoingContext(ctx, md)
}
copied := md.Copy() // avoid racey updates
// overwrite/add 'hasleader' key/value
md.Set(rpctypes.MetadataRequireLeaderKey, rpctypes.MetadataHasLeader)
return metadata.NewOutgoingContext(ctx, md)
copied.Set(rpctypes.MetadataRequireLeaderKey, rpctypes.MetadataHasLeader)
return metadata.NewOutgoingContext(ctx, copied)
}

// embeds client version
Expand All @@ -42,7 +43,8 @@ func withVersion(ctx context.Context) context.Context {
md = metadata.Pairs(rpctypes.MetadataClientAPIVersionKey, version.APIVersion)
return metadata.NewOutgoingContext(ctx, md)
}
copied := md.Copy() // avoid racey updates
// overwrite/add version key/value
md.Set(rpctypes.MetadataClientAPIVersionKey, version.APIVersion)
return metadata.NewOutgoingContext(ctx, md)
copied.Set(rpctypes.MetadataClientAPIVersionKey, version.APIVersion)
return metadata.NewOutgoingContext(ctx, copied)
}

0 comments on commit 45a45d3

Please sign in to comment.