From bc7080337ee4fe7309d019b0907f2b1ef29d3cf0 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 24 Feb 2026 23:02:11 +0200 Subject: [PATCH] check the data again after the GetOrSet write lock --- tools/store/store.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/store/store.go b/tools/store/store.go index 0c395711..bdb5d96a 100644 --- a/tools/store/store.go +++ b/tools/store/store.go @@ -178,15 +178,22 @@ func (s *Store[K, T]) GetOrSet(key K, setFunc func() T) T { s.mu.RLock() v, ok := s.data[key] s.mu.RUnlock() + if ok { + return v + } + // lock again for write + s.mu.Lock() + defer s.mu.Unlock() + + // check again in case it was set between the 2 locks + v, ok = s.data[key] if !ok { - s.mu.Lock() v = setFunc() if s.data == nil { s.data = make(map[K]T) } s.data[key] = v - s.mu.Unlock() } return v