Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example of a test that causes a badgerdb crash with a misleading error string
package main
import (
=== RUN TestBadgerClose
--- FAIL: TestBadgerClose (0.63s)
badger_close_test:62 I am a failed test
panic: runtime error: invalid memory address or nil pointer dereference
panic: Unclosed iterator at time of Txn.Discard. [recovered]
panic: Unclosed iterator at time of Txn.Discard.
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x7b3162]
goroutine 7 [running]:
/go/1.10.0/src/testing/testing.go:742 +0x29d
panic(0x92dde0, 0xa79480)
/go/1.10.0/src/runtime/panic.go:505 +0x229*Txn).Discard(0xc4201685a0)
$HOME/src/go/src/ +0xdc
panic(0x95fc40, 0xd42ef0)
/go/1.10.0/src/runtime/panic.go:505 +0x229*Skiplist).IncrRef(...)
$HOME/src/go/src/*DB).getMemTables(0xc420174700, 0x0, 0x0, 0x0, 0x0)
$HOME/src/go/src/ +0xd2*Txn).NewIterator(0xc4201685a0, 0x0, 0x64, 0x0, 0x0)
$HOME/src/go/src/ +0x68, 0x0, 0x0)
$HOME/src/go/src/ +0x68*DB).Update(0xc420174700, 0xa32aa0, 0x0, 0x0)
$HOME/src/go/src/ +0x84, 0xc420087e90, 0xc4201960f0)
$HOME/src/go/src/ +0x3b
$HOME/src/go/src/ +0x36c
testing.tRunner(0xc4201960f0, 0xa32ab8)
/go/1.10.0/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
/go/1.10.0/src/testing/testing.go:824 +0x2e0
func TestBadgerClose(t *testing.T) {
opts := badger.DefaultOptions
opts.Dir = "/tmp/badger_test"
opts.ValueDir = opts.Dir
if err := os.MkdirAll(opts.Dir, 0775); err != nil {
defer os.RemoveAll(opts.Dir)
db, err := badger.Open(opts)
if err != nil {
defer func() {
err = db.Update(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
if err := txn.Delete(it.Item().Key()); err != nil {
return err
return nil
if err != nil {
// Wrong order. Close happens before Update
defer db.Close()
err = db.Update(func(txn *badger.Txn) error {
for i := 0; i < 10; i++ {
if err := txn.Set([]byte{byte(i)}, []byte("value")); err != nil {
return err
return nil
if err != nil {
t.Error("I am a failed test")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment