diff --git a/CHANGELOG.md b/CHANGELOG.md index 03957b2fc..608699669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Fixed data race in logger ydb/log/WithNames + ## v3.65.1 * Updated dependency `ydb-go-genproto` * Added processing of `Ydb.StatusIds_EXTERNAL_ERROR` in `retry.Retry` diff --git a/log/context.go b/log/context.go index 81231df01..34aed11a7 100644 --- a/log/context.go +++ b/log/context.go @@ -1,6 +1,8 @@ package log -import "context" +import ( + "context" +) type ( ctxLevelKey struct{} @@ -18,7 +20,11 @@ func LevelFromContext(ctx context.Context) Level { } func WithNames(ctx context.Context, names ...string) context.Context { - return context.WithValue(ctx, ctxNamesKey{}, append(NamesFromContext(ctx), names...)) + // trim capacity for force allocate new memory for new slice and prevent data race + oldNames := NamesFromContext(ctx) + oldNames = oldNames[:len(oldNames):len(oldNames)] + + return context.WithValue(ctx, ctxNamesKey{}, append(oldNames, names...)) } func NamesFromContext(ctx context.Context) []string { @@ -27,7 +33,7 @@ func NamesFromContext(ctx context.Context) []string { return []string{} } - return v + return v[:len(v):len(v)] // prevent re } func with(ctx context.Context, lvl Level, names ...string) context.Context {