Panic when concurrently closing DB while some goroutines doing reads

What version of Go are you using (go version)?

go version go1.16.3 darwin/amd64

What operating system are you using?

macOS 11.3
Ubuntu LTS

What version of Badger are you using?


Does this issue reproduce with the latest master?


Steps to Reproduce the issue

package main

import (


func main() {
	dir, err := os.MkdirTemp("", "badger")
	if err != nil {
	defer os.RemoveAll(dir)

	db, err := badger.Open(badger.DefaultOptions(dir))
	if err != nil {

	key := []byte("key")
	err = db.Update(func(txn *badger.Txn) error {
		return txn.Set(key, []byte("value"))
	if err != nil {

	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		i := i
		go func() {
			defer wg.Done()
			for {
				err := db.View(func(txn *badger.Txn) error {
					_, err := txn.Get(key)
					if err == badger.ErrKeyNotFound {
						return nil
					return err
				if err != nil {
					log.Println(i, ": ", err)

	time.Sleep(time.Second * 1)
	err = db.Close()
	if err != nil {


What Badger options were set?


What did you do?

Close the database while concurrently reading a key from multiple goroutines. See the above code…

What did you expect to see?

Reading goroutines to fail with an error (ErrDBClosed) when database is closed. I don’t see any info about this on close() method.

What did you see instead?

A panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4375254]

goroutine 75 [running]:*memTable).IncrRef(...)
        /Users/marco/.go/pkg/mod/*DB).getMemTables(0xc000128900, 0x0, 0x0, 0x0, 0x0)
        /Users/marco/.go/pkg/mod/ +0x2d4*DB).get(0xc000128900, 0xc0074729d0, 0xb, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /Users/marco/.go/pkg/mod/ +0x92*Txn).Get(0xc00747ac00, 0xc0004b1dbc, 0x3, 0x3, 0x43ade79, 0xc0000689c0, 0x1)
        /Users/marco/.go/pkg/mod/ +0x14d
main.main.func2.1(0xc00747ac00, 0x0, 0xc00747ac00)
        /Users/marco/projects/playground/main.go:40 +0x4a*DB).View(0xc000128900, 0xc000178f40, 0x0, 0x0)
        /Users/marco/.go/pkg/mod/ +0x95
main.main.func2(0xc000020160, 0xc000128900, 0xc0004b1dbc, 0x3, 0x3, 0x8)
        /Users/marco/projects/playground/main.go:39 +0xbe
created by main.main
        /Users/marco/projects/playground/main.go:36 +0x365