From 21ea96e3749ba1f58d11021f1bc77cc1ef0cfc32 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:07:05 +0700 Subject: [PATCH] fix: missing lock in DeleteVersionsFrom (backport #642) (#649) Co-authored-by: yihuang --- nodedb.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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 }