From ffb171a38165675b8b8aa279ef88ce956a0a8f9c Mon Sep 17 00:00:00 2001 From: Callum Styan Date: Thu, 4 Jan 2024 17:52:12 -0800 Subject: [PATCH] fix concurrent map write for base map usage Signed-off-by: Callum Styan --- pkg/logql/log/labels.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/logql/log/labels.go b/pkg/logql/log/labels.go index 76a1ae0d7d5e..009354fca79f 100644 --- a/pkg/logql/log/labels.go +++ b/pkg/logql/log/labels.go @@ -146,6 +146,7 @@ type BaseLabelsBuilder struct { // LabelsBuilder is the same as labels.Builder but tailored for this package. type LabelsBuilder struct { base labels.Labels + mapLock sync.RWMutex baseMap map[string]string buf labels.Labels currentResult LabelsResult @@ -497,10 +498,16 @@ func (b *LabelsBuilder) IntoMap(m map[string]string) { func (b *LabelsBuilder) Map() map[string]string { if !b.hasDel() && !b.hasAdd() && !b.HasErr() { - if b.baseMap == nil { - b.baseMap = b.base.Map() + b.mapLock.RLock() + if b.baseMap != nil { + defer b.mapLock.RUnlock() + return b.baseMap } - return b.baseMap + b.mapLock.RUnlock() + b.mapLock.Lock() + b.baseMap = b.base.Map() + b.mapLock.Unlock() + } b.buf = b.UnsortedLabels(b.buf) // todo should we also cache maps since limited by the result ?