From 2b1395a88aed3ee941be35b025fd3431a1afee36 Mon Sep 17 00:00:00 2001 From: knight42 Date: Sat, 5 Sep 2020 02:43:53 +0800 Subject: [PATCH] fix(azure::cache): TimedCache.Getter should be called once on the same key If a key have't been saved in TimedCache, and there are multiple goroutines getting this key, TimedCache.Getter might be invoked multiple times because the entry is overwritten in TimedCache.getInternal. Signed-off-by: knight42 --- .../legacy-cloud-providers/azure/cache/azure_cache.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/cache/azure_cache.go b/staging/src/k8s.io/legacy-cloud-providers/azure/cache/azure_cache.go index 39c679ebc8d..b88fbbad4d7 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/cache/azure_cache.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/cache/azure_cache.go @@ -103,6 +103,15 @@ func (t *TimedCache) getInternal(key string) (*AzureCacheEntry, error) { t.Lock.Lock() defer t.Lock.Unlock() + // Another goroutine might have written the same key. + entry, exists, err = t.Store.GetByKey(key) + if err != nil { + return nil, err + } + if exists { + return entry.(*AzureCacheEntry), nil + } + // Still not found, add new entry with nil data. // Note the data will be filled later by getter. newEntry := &AzureCacheEntry{