Skip to content

Commit

Permalink
impl/memory: Fix memcache stats race.
Browse files Browse the repository at this point in the history
Fix a race in memcache stats that was caused by modification of common
stats varibles while holding a possibly-shared RLock. This fix switches
locking from RWMutex to Mutex, since potentially every access contains
mutations to the shared state.

BUG=None
TEST=local
  - Re-ran external test with fix, race no longer triggered.

R=iannucci@chromium.org

Review-Url: https://codereview.chromium.org/2554793002
  • Loading branch information
danjacques authored and Commit bot committed Dec 6, 2016
1 parent 7941536 commit c12ab6f
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions impl/memory/memcache.go
Expand Up @@ -78,7 +78,7 @@ func (m *mcDataItem) toUserItem(key string) *mcItem {
}

type memcacheData struct {
lock sync.RWMutex
lock sync.Mutex
items map[string]*mcDataItem
casID uint64

Expand Down Expand Up @@ -258,8 +258,8 @@ func (m *memcacheImpl) GetMulti(keys []string, cb mc.RawItemCB) error {

for i, k := range keys {
itms[i], errs[i] = func() (mc.Item, error) {
m.data.lock.RLock()
defer m.data.lock.RUnlock()
m.data.lock.Lock()
defer m.data.lock.Unlock()
val, err := m.data.retrieveLocked(now, k)
if err != nil {
return nil, err
Expand Down Expand Up @@ -345,8 +345,8 @@ func (m *memcacheImpl) Increment(key string, delta int64, initialValue *uint64)
}

func (m *memcacheImpl) Stats() (*mc.Statistics, error) {
m.data.lock.RLock()
defer m.data.lock.RUnlock()
m.data.lock.Lock()
defer m.data.lock.Unlock()

ret := m.data.stats
return &ret, nil
Expand Down

0 comments on commit c12ab6f

Please sign in to comment.