Skip to content

Commit

Permalink
Better lock handling on close
Browse files Browse the repository at this point in the history
(cherry picked from commit 7e773d2)
  • Loading branch information
alpe committed Dec 17, 2024
1 parent f9c402d commit bf2163d
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 @@ -600,7 +600,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 bf2163d

Please sign in to comment.