[#598] Fix use-after-close bug in badger writecache
Some checks failed
DCO action / DCO (pull_request) Successful in 1m36s
Vulncheck / Vulncheck (pull_request) Successful in 3m27s
Build / Build Components (1.21) (pull_request) Successful in 4m15s
Tests and linters / Staticcheck (pull_request) Successful in 4m52s
Build / Build Components (1.20) (pull_request) Successful in 5m40s
Tests and linters / Lint (pull_request) Successful in 5m42s
Tests and linters / Tests with -race (pull_request) Failing after 10m3s
Tests and linters / Tests (1.21) (pull_request) Successful in 11m1s
Tests and linters / Tests (1.20) (pull_request) Successful in 11m10s

Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
This commit is contained in:
Alejandro Lopez 2023-08-14 10:54:09 +03:00
parent 376f03a445
commit bd9145dc8c
2 changed files with 15 additions and 0 deletions

View file

@ -80,6 +80,13 @@ func (c *cache) flushSmallObjects() {
continue
}
// Using the db after Close will panic and badger won't wait for outstanding txs,
// so we need to check manually.
if c.db.IsClosed() {
c.modeMtx.RUnlock()
return
}
_ = c.db.View(func(tx *badger.Txn) error {
it := tx.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()

View file

@ -20,6 +20,14 @@ func (c *cache) runGCLoop() {
case <-c.closeCh:
return
case <-t.C:
// This serves to synchronize the c.db field when changing mode as well.
c.modeMtx.RLock()
ro := c.readOnly()
c.modeMtx.RUnlock()
if ro {
continue
}
// 0.5 is the recommended value so that write amplification of the value log is 2.
// See https://pkg.go.dev/github.com/dgraph-io/badger/v4#DB.RunValueLogGC for more info.
for c.db.RunValueLogGC(0.5) == nil {