Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better lock handling on close #1024

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading