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 20.04.2.0 LTS

What version of Badger are you using?

v3.2011.1

Does this issue reproduce with the latest master?

yes

Steps to Reproduce the issue

package main

import (
	"log"
	"os"
	"sync"
	"time"

	"github.com/dgraph-io/badger/v3"
)

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

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

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

	var wg sync.WaitGroup
	for i := 0; i < 10; i++ {
		i := i
		wg.Add(1)
		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)
					return
				}
			}
		}()
	}

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

	wg.Wait()
}

What Badger options were set?

badger.DefaultOptions(dir)

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]:
github.com/dgraph-io/badger/v3.(*memTable).IncrRef(...)
        /Users/marco/.go/pkg/mod/github.com/dgraph-io/badger/v3@v3.0.0-20210519143323-3b210404e360/memtable.go:231
github.com/dgraph-io/badger/v3.(*DB).getMemTables(0xc000128900, 0x0, 0x0, 0x0, 0x0)
        /Users/marco/.go/pkg/mod/github.com/dgraph-io/badger/v3@v3.0.0-20210519143323-3b210404e360/db.go:701 +0x2d4
github.com/dgraph-io/badger/v3.(*DB).get(0xc000128900, 0xc0074729d0, 0xb, 0xb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /Users/marco/.go/pkg/mod/github.com/dgraph-io/badger/v3@v3.0.0-20210519143323-3b210404e360/db.go:735 +0x92
github.com/dgraph-io/badger/v3.(*Txn).Get(0xc00747ac00, 0xc0004b1dbc, 0x3, 0x3, 0x43ade79, 0xc0000689c0, 0x1)
        /Users/marco/.go/pkg/mod/github.com/dgraph-io/badger/v3@v3.0.0-20210519143323-3b210404e360/txn.go:498 +0x14d
main.main.func2.1(0xc00747ac00, 0x0, 0xc00747ac00)
        /Users/marco/projects/playground/main.go:40 +0x4a
github.com/dgraph-io/badger/v3.(*DB).View(0xc000128900, 0xc000178f40, 0x0, 0x0)
        /Users/marco/.go/pkg/mod/github.com/dgraph-io/badger/v3@v3.0.0-20210519143323-3b210404e360/txn.go:828 +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