From ce42e9ea1f4efd6fe678f1fd4130ff31a6f852c6 Mon Sep 17 00:00:00 2001 From: Vishal Nayak Date: Fri, 1 Mar 2019 20:30:14 -0500 Subject: [PATCH] Add locking around base context (#6321) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Got offline 👍 from Calvin. --- command/agent/cache/lease_cache.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/command/agent/cache/lease_cache.go b/command/agent/cache/lease_cache.go index bccf695f2..4f1310d65 100644 --- a/command/agent/cache/lease_cache.go +++ b/command/agent/cache/lease_cache.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "net/http" "strings" + "sync" "github.com/hashicorp/errwrap" hclog "github.com/hashicorp/go-hclog" @@ -69,6 +70,7 @@ type LeaseCache struct { logger hclog.Logger db *cachememdb.CacheMemDB baseCtxInfo *cachememdb.ContextInfo + l *sync.RWMutex } // LeaseCacheConfig is the configuration for initializing a new @@ -108,6 +110,7 @@ func NewLeaseCache(conf *LeaseCacheConfig) (*LeaseCache, error) { logger: conf.Logger, db: db, baseCtxInfo: baseCtxInfo, + l: &sync.RWMutex{}, }, nil } @@ -305,7 +308,9 @@ func (c *LeaseCache) Send(ctx context.Context, req *SendRequest) (*SendResponse, func (c *LeaseCache) createCtxInfo(ctx context.Context) *cachememdb.ContextInfo { if ctx == nil { + c.l.RLock() ctx = c.baseCtxInfo.Ctx + c.l.RUnlock() } return cachememdb.NewContextInfo(ctx) } @@ -500,14 +505,15 @@ func (c *LeaseCache) handleCacheClear(ctx context.Context, clearType string, cle // Cancel the base context which triggers all the goroutines to // stop and evict entries from cache. c.logger.Debug("canceling base context") + c.l.Lock() c.baseCtxInfo.CancelFunc() - // Reset the base context baseCtx, baseCancel := context.WithCancel(ctx) c.baseCtxInfo = &cachememdb.ContextInfo{ Ctx: baseCtx, CancelFunc: baseCancel, } + c.l.Unlock() // Reset the memdb instance if err := c.db.Flush(); err != nil {