From 3b3604e3621dbcfd1d383290a21db577a258df63 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Mon, 9 Jan 2017 12:12:31 -0800 Subject: [PATCH] Fix race in (*tsm1.Cache).values Without this read lock, this race would happen during a concurrent snapshot compaction and query. --- tsdb/engine/tsm1/cache.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tsdb/engine/tsm1/cache.go b/tsdb/engine/tsm1/cache.go index b166cc398b..12e767fb19 100644 --- a/tsdb/engine/tsm1/cache.go +++ b/tsdb/engine/tsm1/cache.go @@ -554,14 +554,18 @@ func (c *Cache) SetMaxSize(size uint64) { c.mu.Unlock() } -// values returns the values for the key. It doesn't lock and assumes the data is -// already sorted. Should only be used in compact.go in the CacheKeyIterator +// values returns the values for the key. It assumes the data is already sorted. +// It doesn't lock the cache but it does read-lock the entry if there is one for the key. +// values should only be used in compact.go in the CacheKeyIterator. func (c *Cache) values(key string) Values { e, _ := c.store.entry(key) if e == nil { return nil } - return e.values + e.mu.RLock() + v := e.values + e.mu.RUnlock() + return v } // ApplyEntryFn applies the function f to each entry in the Cache.