namespace: Copy ttrpc metadata before setting header
If there are multiple goroutines calling `namespace.WithNamespace` on a ctx that already had namespace, there will be a data race when `withTTRPCNamespaceHeader` calling `MD.Set()`. So we have to copy the md before using it. Signed-off-by: Li Yuxuan <liyuxuan04@baidu.com>
This commit is contained in:
@@ -27,10 +27,20 @@ const (
|
||||
TTRPCHeader = "containerd-namespace-ttrpc"
|
||||
)
|
||||
|
||||
func copyMetadata(src ttrpc.MD) ttrpc.MD {
|
||||
md := ttrpc.MD{}
|
||||
for k, v := range src {
|
||||
md[k] = append(md[k], v...)
|
||||
}
|
||||
return md
|
||||
}
|
||||
|
||||
func withTTRPCNamespaceHeader(ctx context.Context, namespace string) context.Context {
|
||||
md, ok := ttrpc.GetMetadata(ctx)
|
||||
if !ok {
|
||||
md = ttrpc.MD{}
|
||||
} else {
|
||||
md = copyMetadata(md)
|
||||
}
|
||||
md.Set(TTRPCHeader, namespace)
|
||||
return ttrpc.WithMetadata(ctx, md)
|
||||
|
||||
Reference in New Issue
Block a user