From 646fbcc22880e05f66656b09510a6942a86fca82 Mon Sep 17 00:00:00 2001 From: corver Date: Sun, 20 Oct 2024 11:01:43 +0200 Subject: [PATCH 1/2] fix data race --- immutable_tree.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/immutable_tree.go b/immutable_tree.go index 54ccdf991..932db7edd 100644 --- a/immutable_tree.go +++ b/immutable_tree.go @@ -190,10 +190,15 @@ func (t *ImmutableTree) Get(key []byte) ([]byte, error) { } if fastNode == nil { + latestVersion, err := t.ndb.getLatestVersion() + if err != nil { + return nil, err + } + // If the tree is of the latest version and fast node is not in the tree // then the regular node is not in the tree either because fast node // represents live state. - if t.version == t.ndb.latestVersion { + if t.version == latestVersion { return nil, nil } From 52d81e7cf264b4e95338e878fc284909f1672d6b Mon Sep 17 00:00:00 2001 From: corver Date: Thu, 7 Nov 2024 11:42:13 +0200 Subject: [PATCH 2/2] use safeGetLatestVersion --- immutable_tree.go | 7 +------ nodedb.go | 9 +++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/immutable_tree.go b/immutable_tree.go index 932db7edd..18a9929fc 100644 --- a/immutable_tree.go +++ b/immutable_tree.go @@ -190,15 +190,10 @@ func (t *ImmutableTree) Get(key []byte) ([]byte, error) { } if fastNode == nil { - latestVersion, err := t.ndb.getLatestVersion() - if err != nil { - return nil, err - } - // If the tree is of the latest version and fast node is not in the tree // then the regular node is not in the tree either because fast node // represents live state. - if t.version == latestVersion { + if t.version == t.ndb.safeGetLatestVersion() { return nil, nil } diff --git a/nodedb.go b/nodedb.go index 829c5697d..fec69df6c 100644 --- a/nodedb.go +++ b/nodedb.go @@ -797,6 +797,15 @@ func (ndb *nodeDB) resetLegacyLatestVersion(version int64) { ndb.legacyLatestVersion = version } +// safeGetLatestVersion returns the value of ndb.latestVersion directly +// without additional logic performed by getLatestVersion. +func (ndb *nodeDB) safeGetLatestVersion() int64 { + ndb.mtx.Lock() + defer ndb.mtx.Unlock() + + return ndb.latestVersion +} + func (ndb *nodeDB) getLatestVersion() (int64, error) { ndb.mtx.Lock() latestVersion := ndb.latestVersion