diff --git a/nodedb.go b/nodedb.go index 2d4686fe6..62c56d1ec 100644 --- a/nodedb.go +++ b/nodedb.go @@ -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 } @@ -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) @@ -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 }