From 6489a0dd1f98e9ce1c64c2eae93c8a88df7ae674 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik <10907694+magicxyyz@users.noreply.github.com> Date: Tue, 21 Nov 2023 14:22:22 +0000 Subject: [PATCH] ethdb/pebble: don't double-close iterator inside pebbleIterator (#28566) Adds 'released' flag to pebbleIterator to avoid double closing cockroachdb/pebble.Iterator as it is an invalid operation. Fixes #28565 --- ethdb/pebble/pebble.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ethdb/pebble/pebble.go b/ethdb/pebble/pebble.go index 6d0ea9496..d58329c6d 100644 --- a/ethdb/pebble/pebble.go +++ b/ethdb/pebble/pebble.go @@ -609,9 +609,12 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error { // pebbleIterator is a wrapper of underlying iterator in storage engine. // The purpose of this structure is to implement the missing APIs. +// +// The pebble iterator is not thread-safe. type pebbleIterator struct { - iter *pebble.Iterator - moved bool + iter *pebble.Iterator + moved bool + released bool } // NewIterator creates a binary-alphabetical iterator over a subset @@ -623,7 +626,7 @@ func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator { UpperBound: upperBound(prefix), }) iter.First() - return &pebbleIterator{iter: iter, moved: true} + return &pebbleIterator{iter: iter, moved: true, released: false} } // Next moves the iterator to the next key/value pair. It returns whether the @@ -658,4 +661,9 @@ func (iter *pebbleIterator) Value() []byte { // Release releases associated resources. Release should always succeed and can // be called multiple times without causing error. -func (iter *pebbleIterator) Release() { iter.iter.Close() } +func (iter *pebbleIterator) Release() { + if !iter.released { + iter.iter.Close() + iter.released = true + } +}