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: missing lock in DeleteVersionsFrom (backport #642) #649

Merged
merged 1 commit into from
Dec 16, 2022
Merged
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: 8 additions & 1 deletion nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ func newNodeDB(db dbm.DB, cacheSize int, opts *Options) *nodeDB {
func (ndb *nodeDB) GetNode(hash []byte) (*Node, error) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
return ndb.unsafeGetNode(hash)
}

// Contract: the caller should hold the ndb.mtx lock.
func (ndb *nodeDB) unsafeGetNode(hash []byte) (*Node, error) {
if len(hash) == 0 {
return nil, ErrNodeMissingHash
}
Expand Down Expand Up @@ -435,6 +439,9 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
return errors.Errorf("root for version %v not found", latest)
}

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

for v, r := range ndb.versionReaders {
if v >= version && r != 0 {
return errors.Errorf("unable to delete version %v with %v active readers", v, r)
Expand Down Expand Up @@ -600,7 +607,7 @@ func (ndb *nodeDB) deleteNodesFrom(version int64, hash []byte) error {
return nil
}

node, err := ndb.GetNode(hash)
node, err := ndb.unsafeGetNode(hash)
if err != nil {
return err
}
Expand Down