Skip to content

Commit 415adfb

Browse files
authored
lib/db: Don't hang on double iterator release with Badger (syncthing#6960)
Since iterators must be released before committing or discarding a transaction we have the pattern of both deferring a release plus doing it manually. But we can't release twice because we track this with a WaitGroup that will panic when there are more Done()s than Add()s. This just adds a boolean to let an iterator keep track.
1 parent 3dd13c3 commit 415adfb

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/db/backend/badger_backend.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ type badgerIterator struct {
334334
first []byte
335335
last []byte
336336
releaseFn func()
337+
released bool
337338
didSeek bool
338339
err error
339340
}
@@ -397,6 +398,12 @@ func (i *badgerIterator) Error() error {
397398
}
398399

399400
func (i *badgerIterator) Release() {
401+
if i.released {
402+
// We already closed this iterator, no need to do it again
403+
// (and the releaseFn might hang if we do).
404+
return
405+
}
406+
i.released = true
400407
i.it.Close()
401408
if i.releaseFn != nil {
402409
i.releaseFn()

0 commit comments

Comments
 (0)