From 834898af1a3c1e5f789e9c18bd51bdb9fef0f84f Mon Sep 17 00:00:00 2001 From: pmahindrakar-oss Date: Thu, 23 May 2024 00:03:48 -0700 Subject: [PATCH] Lock the locker in the wait to adher to cond.Wait() semantics Signed-off-by: pmahindrakar-oss --- .../clients/go/admin/cache/token_cache_inmemory.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/flyteidl/clients/go/admin/cache/token_cache_inmemory.go b/flyteidl/clients/go/admin/cache/token_cache_inmemory.go index 7136a6e3fe..21e16787ed 100644 --- a/flyteidl/clients/go/admin/cache/token_cache_inmemory.go +++ b/flyteidl/clients/go/admin/cache/token_cache_inmemory.go @@ -24,7 +24,6 @@ func (t *TokenCacheInMemoryProvider) GetToken() (*oauth2.Token, error) { if tkn == nil { return nil, fmt.Errorf("cannot find token in cache") } - return tkn.(*oauth2.Token), nil } @@ -46,8 +45,12 @@ func (t *TokenCacheInMemoryProvider) Unlock() { } // CondWait waits for the condition to be true. +// It also locks the Locker in the condition variable as the semantics of Wait is that it unlocks the Locker after adding +// the consumer to the waitlist and before blocking on notification. func (t *TokenCacheInMemoryProvider) CondWait() { + t.cond.L.Lock() t.cond.Wait() + t.cond.L.Unlock() } // CondBroadcast signals the condition. @@ -56,10 +59,9 @@ func (t *TokenCacheInMemoryProvider) CondBroadcast() { } func NewTokenCacheInMemoryProvider() *TokenCacheInMemoryProvider { - condMutex := &sync.Mutex{} return &TokenCacheInMemoryProvider{ - mu: condMutex, + mu: &sync.Mutex{}, token: atomic.Value{}, - cond: sync.NewCond(condMutex), + cond: sync.NewCond(&sync.Mutex{}), } }