Skip to content

Commit bf2163d

Browse files
committed
Better lock handling on close
(cherry picked from commit 7e773d2)
1 parent f9c402d commit bf2163d

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

nodedb.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func (ndb *nodeDB) startPruning() {
600600
for {
601601
select {
602602
case <-ndb.ctx.Done():
603-
ndb.done <- struct{}{}
603+
close(ndb.done)
604604
return
605605
default:
606606
ndb.mtx.Lock()
@@ -1121,14 +1121,15 @@ func (ndb *nodeDB) traverseOrphans(prevVersion, curVersion int64, fn func(*Node)
11211121

11221122
// Close the nodeDB.
11231123
func (ndb *nodeDB) Close() error {
1124-
ndb.mtx.Lock()
1125-
defer ndb.mtx.Unlock()
1126-
11271124
ndb.cancel()
1125+
11281126
if ndb.opts.AsyncPruning {
11291127
<-ndb.done // wait for the pruning process to finish
11301128
}
11311129

1130+
ndb.mtx.Lock()
1131+
defer ndb.mtx.Unlock()
1132+
11321133
if ndb.batch != nil {
11331134
if err := ndb.batch.Close(); err != nil {
11341135
return err

nodedb_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,5 @@ func TestCloseNodeDB(t *testing.T) {
444444
opts.AsyncPruning = true
445445
ndb := newNodeDB(db, 0, opts, NewNopLogger())
446446
require.NoError(t, ndb.Close())
447+
require.NoError(t, ndb.Close()) // must not block or fail on second call
447448
}

0 commit comments

Comments
 (0)