Skip to content

Commit

Permalink
fix: missing lock in DeleteVersionsFrom (backport #642) (#649)
Browse files Browse the repository at this point in the history
Co-authored-by: yihuang <huang@crypto.com>
  • Loading branch information
mergify[bot] and yihuang authored Dec 16, 2022
1 parent 2af006a commit 21ea96e
Showing 1 changed file with 8 additions and 1 deletion.
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

0 comments on commit 21ea96e

Please sign in to comment.