Skip to content

Commit

Permalink
fix: missing lock in DeleteVersionsFrom (backport cosmos#642) (cosmos…
Browse files Browse the repository at this point in the history
…#649)

Co-authored-by: yihuang <[email protected]>
  • Loading branch information
2 people authored and ankurdotb committed Feb 28, 2023
1 parent f665c40 commit 83fe43a
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 @@ -605,7 +612,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 83fe43a

Please sign in to comment.