karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.29k stars 120 forks source link

Data race in cache.Clear #49

Closed bep closed 4 years ago

bep commented 4 years ago

//this isn't thread safe. It's meant to be called from non-concurrent tests

But even in non-concurrents tests, go test -race reports a race:

WARNING: DATA RACE
Write at 0x00c000162800 by goroutine 23:
  github.com/karlseguin/ccache/v2.(*LayeredCache).Clear()
      /Users/bep/go/pkg/mod/github.com/karlseguin/ccache/v2@v2.0.7-0.20200814031513-0dbf3f125f13/layeredcache.go:172 +0xb4
  github.com/gohugoio/hugo/cache/memcache.(*Cache).Clear()
      /Users/bep/dev/go/gohugoio/hugo/cache/memcache/memcache.go:211 +0x63c
  github.com/gohugoio/hugo/cache/memcache.TestCache()
      /Users/bep/dev/go/gohugoio/hugo/cache/memcache/memcache_test.go:47 +0x52b
  testing.tRunner()
      /Users/bep/dev/go/dump/go/src/testing/testing.go:1109 +0x202

Previous write at 0x00c000162800 by goroutine 31:
  github.com/karlseguin/ccache/v2.(*LayeredCache).doPromote()
      /Users/bep/go/pkg/mod/github.com/karlseguin/ccache/v2@v2.0.7-0.20200814031513-0dbf3f125f13/layeredcache.go:269 +0x50e
  github.com/karlseguin/ccache/v2.(*LayeredCache).worker()
      /Users/bep/go/pkg/mod/github.com/karlseguin/ccache/v2@v2.0.7-0.20200814031513-0dbf3f125f13/layeredcache.go:229 +0x8d3
karlseguin commented 4 years ago

With the Control channel that was recently added, making Clear thread-safe became pretty easy. So, that's what I did. https://github.com/karlseguin/ccache/commit/1189f7f993b53ed61b201dec120063d37638f9e9

go test -race did not output an issue for the old code (for Clear) for me though, so I'm not 100% sure it'll address the error that you're saying, but I think it will.