ReadOnly Stream panic

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

$ go version go1.16 linux/amd64

What operating system are you using?

Ubuntu 20.04.2 LTS

What version of Badger are you using?

github.com/dgraph-io/badger/v3 v3.2011.1

Does this issue reproduce with the latest master?

yes

Steps to Reproduce the issue

	streamFunc := func(db *badger.DB, prefix []byte) {
		stream := db.NewStream()
		stream.Prefix = prefix
		stream.Send = func(buf *z.Buffer) error { return nil }
		stream.Orchestrate(context.Background())
	}

	db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true).WithReadOnly(false))
	assert.NoError(t, err)

	assert.NotPanics(t, func() { streamFunc(db, []byte("")) }) // OK
	assert.NotPanics(t, func() { streamFunc(db, []byte("not_a_prefix")) }) // OK

	db, err = badger.Open(badger.DefaultOptions("").WithInMemory(true).WithReadOnly(true))
	assert.NoError(t, err)

	assert.NotPanics(t, func() { streamFunc(db, []byte("")) }) // Not OK
	assert.NotPanics(t, func() { streamFunc(db, []byte("not_a_prefix")) }) // Not OK

What Badger options were set?

See above in example

What did you do?

Ran stream on a readonly database scanning over a prefix which is not present

What did you expect to see?

The stream return with no matching entries

What did you see instead?

Badger panics - can’t catch with the assert package. However it works as expected when I open the database with ReadOnly == false

Thanks for reporting. This has been fixes by fix(iterator): fix iterator when data does not exist in read only mode by NamanJain8 · Pull Request #1670 · dgraph-io/badger · GitHub