Skip to content

Commit

Permalink
fix: Better lock handling on close (backport #1024) (#1025)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Peters <alpe@users.noreply.github.com>
  • Loading branch information
mmsqe and alpe authored Dec 18, 2024
1 parent b7bf354 commit 3b0ad99
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ func (ndb *nodeDB) startPruning() {
for {
select {
case <-ndb.ctx.Done():
ndb.done <- struct{}{}
close(ndb.done)
return
default:
ndb.mtx.Lock()
Expand Down Expand Up @@ -1121,14 +1121,15 @@ func (ndb *nodeDB) traverseOrphans(prevVersion, curVersion int64, fn func(*Node)

// Close the nodeDB.
func (ndb *nodeDB) Close() error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

ndb.cancel()

if ndb.opts.AsyncPruning {
<-ndb.done // wait for the pruning process to finish
}

ndb.mtx.Lock()
defer ndb.mtx.Unlock()

if ndb.batch != nil {
if err := ndb.batch.Close(); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions nodedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,5 @@ func TestCloseNodeDB(t *testing.T) {
opts.AsyncPruning = true
ndb := newNodeDB(db, 0, opts, NewNopLogger())
require.NoError(t, ndb.Close())
require.NoError(t, ndb.Close()) // must not block or fail on second call
}

0 comments on commit 3b0ad99

Please sign in to comment.