diff --git a/nodedb.go b/nodedb.go index 1dad7e23f..7ad69af28 100644 --- a/nodedb.go +++ b/nodedb.go @@ -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() @@ -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 diff --git a/nodedb_test.go b/nodedb_test.go index f87103025..ace9b052e 100644 --- a/nodedb_test.go +++ b/nodedb_test.go @@ -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 }