Merge pull request #11706 from gyuho/fix-race

clientv3: fix racy writes to context key
This commit is contained in:
Sahdev Zala 2020-03-19 10:34:08 -04:00 committed by GitHub
commit 45a45d3c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

1
.words
View File

@ -46,6 +46,7 @@ mutex
prefetching
protobuf
prometheus
racey
rafthttp
repin
rpc

View File

@ -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
@ -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)
}