Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unprotected read data race #998

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

corverroos
Copy link
Contributor

@corverroos corverroos commented Oct 20, 2024

This fixes a data race in immutable tree that accesses nodeDB.latestVersion without acquiring the lock.

Summary by CodeRabbit

  • New Features

    • Enhanced version checking in the Get method to ensure it accurately reflects the latest data state.
    • Introduced a new method for efficient retrieval of the latest version, improving performance in frequent access scenarios.
    • Improved error handling for database interactions during version retrieval.
  • Bug Fixes

    • Resolved issues related to version comparison logic, ensuring more reliable results.

@corverroos corverroos requested a review from a team as a code owner October 20, 2024 09:06
Copy link

coderabbitai bot commented Oct 20, 2024

Walkthrough

The changes involve modifications to the Get method of the ImmutableTree struct in the immutable_tree.go file. The direct access to t.ndb.latestVersion has been replaced with a call to t.ndb.safeGetLatestVersion(), enhancing the accuracy of version comparisons. Additionally, error handling within the Get method has been improved to ensure proper propagation of errors during the retrieval of the latest version. A new method, safeGetLatestVersion, has been introduced in the nodeDB struct in nodedb.go, providing thread-safe access to the latestVersion.

Changes

File Change Summary
immutable_tree.go Modified Get method to use safeGetLatestVersion() for version checking and improved error handling.
nodedb.go Added safeGetLatestVersion() method for thread-safe access to latestVersion.

Poem

In the tree where data grows,
A version check now flows,
With latest found, we stand so tall,
Errors caught, we won't let fall.
Hopping through the code so bright,
Our tree's now set, all feels just right! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@corverroos
Copy link
Contributor Author

corverroos commented Oct 20, 2024

We switched from using the cometBFT ABCI RPC to using CosmosSDK gRPC to query application state in this commit.

Our CI then started failing with the following data race:

==================
WARNING: DATA RACE
Write at 0x00c002c7a758 by goroutine 201:
  github.com/cosmos/iavl.(*nodeDB).resetLatestVersion()
      /home/runner/go/pkg/mod/github.com/omni-network/iavl@v1.9.11/nodedb.go:751 +0x99
  github.com/cosmos/iavl.(*MutableTree).SaveVersion()
      /home/runner/go/pkg/mod/github.com/omni-network/iavl@v1.9.11/mutable_tree.go:763 +0x108b
  cosmossdk.io/store/iavl.(*Store).Commit()
      /home/runner/go/pkg/mod/cosmossdk.io/store@v1.1.1/iavl/store.go:126 +0x10e
  cosmossdk.io/store/cache.(*CommitKVStoreCache).Commit()
      <autogenerated>:1 +0x43
  cosmossdk.io/store/rootmulti.commitStores()
      /home/runner/go/pkg/mod/cosmossdk.io/store@v1.1.1/rootmulti/store.go:1196 +0x196
  cosmossdk.io/store/rootmulti.(*Store).Commit()
      /home/runner/go/pkg/mod/cosmossdk.io/store@v1.1.1/rootmulti/store.go:482 +0x39b
  github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).Commit()
      /home/runner/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.50.10/baseapp/abci.go:933 +0x3da
  github.com/omni-network/omni/halo/app.(*App).Commit()
      <autogenerated>:1 +0x52
  github.com/cosmos/cosmos-sdk/server.cometABCIWrapper.Commit()
      /home/runner/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.50.10/server/cmt_abci.go:56 +0x4a
  github.com/cosmos/cosmos-sdk/server.(*cometABCIWrapper).Commit()
      <autogenerated>:1 +0x1b
  github.com/omni-network/omni/halo/app.abciWrapper.Commit()
      /home/runner/work/omni/omni/halo/app/abci.go:160 +0xa5
  github.com/omni-network/omni/halo/app.(*abciWrapper).Commit()
      <autogenerated>:1 +0x7b
  github.com/cometbft/cometbft/abci/client.(*localClient).Commit()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/abci/client/local_client.go:113 +0x158
  github.com/cometbft/cometbft/proxy.(*appConnConsensus).Commit()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/proxy/app_conn.go:109 +0x2cb
  github.com/cometbft/cometbft/state.(*BlockExecutor).Commit()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/state/execution.go:404 +0x368
  github.com/cometbft/cometbft/state.(*BlockExecutor).applyBlock()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/state/execution.go:290 +0x1a39
  github.com/cometbft/cometbft/state.(*BlockExecutor).ApplyVerifiedBlock()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/state/execution.go:202 +0x143e
  github.com/cometbft/cometbft/consensus.(*State).finalizeCommit()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:1772 +0x1214
  github.com/cometbft/cometbft/consensus.(*State).tryFinalizeCommit()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:1682 +0x4a4
  github.com/cometbft/cometbft/consensus.(*State).enterCommit.func1()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:1617 +0x114
  runtime.deferreturn()
      /opt/hostedtoolcache/go/1.23.2/x64/src/runtime/panic.go:605 +0x5d
  github.com/cometbft/cometbft/consensus.(*State).addVote()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:2335 +0x36c4
  github.com/cometbft/cometbft/consensus.(*State).tryAddVote()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:2067 +0x6b
  github.com/cometbft/cometbft/consensus.(*State).handleMsg()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:929 +0x60f
  github.com/cometbft/cometbft/consensus.(*State).receiveRoutine()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:856 +0x751
  github.com/cometbft/cometbft/consensus.(*State).OnStart.gowrap1()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:398 +0x35

Previous read at 0x00c002c7a758 by goroutine 848:
  github.com/cosmos/iavl.(*ImmutableTree).Get()
      /home/runner/go/pkg/mod/github.com/omni-network/iavl@v1.9.11/immutable_tree.go:198 +0x287
  cosmossdk.io/store/iavl.(*immutableTree).Get()
      <autogenerated>:1 +0x64
  cosmossdk.io/store/iavl.(*Store).Get()
      /home/runner/go/pkg/mod/cosmossdk.io/store@v1.1.1/iavl/store.go:200 +0x144
  cosmossdk.io/store/cachekv.(*Store).Get()
      /home/runner/go/pkg/mod/cosmossdk.io/store@v1.1.1/cachekv/store.go:61 +0x26a
  cosmossdk.io/store/gaskv.(*Store).Get()
      /home/runner/go/pkg/mod/cosmossdk.io/store@v1.1.1/gaskv/store.go:37 +0xad
  github.com/cosmos/cosmos-sdk/runtime.coreKVStore.Get()
      /home/runner/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.50.10/runtime/store.go:65 +0x6f
  github.com/cosmos/cosmos-sdk/runtime.(*coreKVStore).Get()
      <autogenerated>:1 +0x1f
  cosmossdk.io/orm/model/ormtable.primaryKeyIndex.getByKeyBytes()
      /home/runner/go/pkg/mod/cosmossdk.io/orm@v1.0.0-beta.3/model/ormtable/primary_key.go:182 +0x95
  cosmossdk.io/orm/model/ormtable.primaryKeyIndex.get()
      /home/runner/go/pkg/mod/cosmossdk.io/orm@v1.0.0-beta.3/model/ormtable/primary_key.go:77 +0x1c7
  cosmossdk.io/orm/model/ormtable.primaryKeyIndex.Get()
      /home/runner/go/pkg/mod/cosmossdk.io/orm@v1.0.0-beta.3/model/ormtable/primary_key.go:68 +0x1b1
  cosmossdk.io/orm/model/ormtable.(*primaryKeyIndex).Get()
      <autogenerated>:1 +0x156
  github.com/omni-network/omni/halo/portal/keeper.blockTable.Get()
      /home/runner/work/omni/omni/halo/portal/keeper/portal.cosmos_orm.go:112 +0x161
  github.com/omni-network/omni/halo/portal/keeper.(*blockTable).Get()
      <autogenerated>:1 +0x6b
  github.com/omni-network/omni/halo/portal/keeper.Keeper.getBlockAndMsgs()
      /home/runner/work/omni/omni/halo/portal/keeper/keeper.go:109 +0xa1
  github.com/omni-network/omni/halo/portal/keeper.Keeper.Block()
      /home/runner/work/omni/omni/halo/portal/keeper/query.go:26 +0x166
  github.com/omni-network/omni/halo/portal/keeper.(*Keeper).Block()
      <autogenerated>:1 +0xcf
  github.com/omni-network/omni/halo/portal/types._Query_Block_Handler.func1()
      /home/runner/work/omni/omni/halo/portal/types/query.pb.go:325 +0xbe
  github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).RegisterGRPCServer.func1()
      /home/runner/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.50.10/baseapp/grpcserver.go:67 +0x4eb
  github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).RegisterGRPCServer.func2.ChainUnaryServer.2.1()
      /home/runner/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.4.0/chain.go:48 +0xd0
  github.com/grpc-ecosystem/go-grpc-middleware/recovery.UnaryServerInterceptor.func1()
      /home/runner/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.4.0/recovery/interceptors.go:33 +0x174
  github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).RegisterGRPCServer.func2.ChainUnaryServer.2()
      /home/runner/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.4.0/chain.go:53 +0x22c
  github.com/omni-network/omni/halo/portal/types._Query_Block_Handler()
      /home/runner/work/omni/omni/halo/portal/types/query.pb.go:327 +0x1f3
  github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).RegisterGRPCServer.func2()
      /home/runner/go/pkg/mod/github.com/cosmos/cosmos-sdk@v0.50.10/baseapp/grpcserver.go:81 +0x1b5
  google.golang.org/grpc.(*Server).processUnaryRPC()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/server.go:1[394](https://github.com/omni-network/omni/actions/runs/11408111384/job/31745600674#step:6:395) +0x1b4b
  google.golang.org/grpc.(*Server).handleStream()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/server.go:1805 +0x1824
  google.golang.org/grpc.(*Server).serveStreams.func2.1()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/server.go:1029 +0x158

Goroutine 201 (running) created at:
  github.com/cometbft/cometbft/consensus.(*State).OnStart()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/state.go:[398](https://github.com/omni-network/omni/actions/runs/11408111384/job/31745600674#step:6:399) +0x22a
  github.com/cometbft/cometbft/libs/service.(*BaseService).Start()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/libs/service/service.go:144 +0x583
  github.com/cometbft/cometbft/consensus.(*Reactor).OnStart()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/consensus/reactor.go:84 +0x214
  github.com/cometbft/cometbft/libs/service.(*BaseService).Start()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/libs/service/service.go:144 +0x583
  github.com/cometbft/cometbft/consensus.(*Reactor).Start()
      <autogenerated>:1 +0x31
  github.com/cometbft/cometbft/p2p.(*Switch).OnStart()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/p2p/switch.go:237 +0x115
  github.com/cometbft/cometbft/libs/service.(*BaseService).Start()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/libs/service/service.go:144 +0x583
  github.com/cometbft/cometbft/node.(*Node).OnStart()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/node/node.go:558 +0x70b
  github.com/cometbft/cometbft/libs/service.(*BaseService).Start()
      /home/runner/go/pkg/mod/github.com/cometbft/cometbft@v0.38.12/libs/service/service.go:144 +0x583
  github.com/omni-network/omni/halo/app.Start()
      /home/runner/work/omni/omni/halo/app/start.go:207 +0x1784
  github.com/omni-network/omni/halo/app_test.TestPruningHistory()
      /home/runner/work/omni/omni/halo/app/pruning_test.go:38 +0x207
  testing.tRunner()
      /opt/hostedtoolcache/go/1.23.2/x64/src/testing/testing.go:1690 +0x226
  testing.(*T).Run.gowrap1()
      /opt/hostedtoolcache/go/1.23.2/x64/src/testing/testing.go:1743 +0x44

Goroutine 848 (finished) created at:
  google.golang.org/grpc.(*Server).serveStreams.func2()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/server.go:1040 +0x224
  google.golang.org/grpc/internal/transport.(*http2Server).operateHeaders()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/internal/transport/http2_server.go:630 +0x3881
  google.golang.org/grpc/internal/transport.(*http2Server).HandleStreams()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/internal/transport/http2_server.go:671 +0x[415](https://github.com/omni-network/omni/actions/runs/11408111384/job/31745600674#step:6:416)
  google.golang.org/grpc.(*Server).serveStreams()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/server.go:1023 +0x69b
  google.golang.org/grpc.(*Server).handleRawConn.func1()
      /home/runner/go/pkg/mod/google.golang.org/grpc@v1.67.1/server.go:958 +0x86
==================

@@ -190,10 +190,15 @@ func (t *ImmutableTree) Get(key []byte) ([]byte, error) {
}

if fastNode == nil {
latestVersion, err := t.ndb.getLatestVersion()
Copy link
Contributor Author

@corverroos corverroos Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that getLatestVersion() will not return t.ndb.latestVersion if it is 0. I'm not sure if this is a problem or not. This change might therefore result in different logic. We could also add a simple getter that only returns t.ndb.latestVersion directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is a solution for the above issue. t.ndb.latestVersion should be set within LoadVersion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to resolve the data race issue, seems like we need to add the API safeGetLatestVersion locked by ndb.mtx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there is already an exiting method ImmutableTree::isLatestVersion which does what my suggestion does, maybe we should just call that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cool-develope I did the change as you suggested using a new safeGetLatestVersion method instead.

@corverroos
Copy link
Contributor Author

@tac0turtle @cool-develope just bumping this as it is blocking us at omni.network to switch to using Cosmos GRPC directly for state queries as it breaks our CI currently. Just an indication of if the fix is good is sufficient, we can then update our fork and move on. Thanks 🙏

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
nodedb.go (2)

800-802: Add documentation about thread-safety guarantees.

Consider adding a comment explaining that this method provides thread-safe access to latestVersion and how it differs from getLatestVersion().

 // safeGetLatestVersion returns the value of ndb.latestVersion directly
 // without additional logic performed by getLatestVersion.
+// This method is thread-safe and should be used when only the current value
+// of latestVersion is needed without the initialization logic present in getLatestVersion.
 func (ndb *nodeDB) safeGetLatestVersion() int64 {

800-808: Consider using sync.RWMutex for better read concurrency.

Since latestVersion is frequently read but less frequently modified, using sync.RWMutex could improve performance by allowing multiple concurrent readers.

This would allow multiple goroutines to read latestVersion simultaneously while still maintaining exclusive access for writers. Consider:

-	mtx                 sync.Mutex                 // Read/write lock.
+	mtx                 sync.RWMutex              // Read/write lock allowing multiple readers.

 func (ndb *nodeDB) safeGetLatestVersion() int64 {
-	ndb.mtx.Lock()
-	defer ndb.mtx.Unlock()
+	ndb.mtx.RLock()
+	defer ndb.mtx.RUnlock()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 646fbcc and 52d81e7.

📒 Files selected for processing (2)
  • immutable_tree.go (1 hunks)
  • nodedb.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • immutable_tree.go

Comment on lines +800 to +808
// 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
}

Copy link

@coderabbitai coderabbitai bot Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Direct accesses to latestVersion need to be protected

Several unprotected accesses to latestVersion were found that could lead to data races:

  • In nodedb_test.go: Multiple direct writes and reads to ndb.latestVersion without mutex protection
  • In import.go: Direct read of tree.ndb.latestVersion without mutex protection
  • In mutable_tree.go: Direct read in condition version == tree.ndb.latestVersion

These locations should be updated to use the new safeGetLatestVersion() method or ensure proper mutex synchronization.

🔗 Analysis chain

LGTM! Thread-safe implementation for accessing latestVersion.

The new method correctly implements thread-safe access to latestVersion using mutex synchronization.

Let's verify that all direct accesses to latestVersion have been replaced with this new safe method:


Let me analyze the direct accesses to verify if they are properly protected. I'll check the mutex usage patterns in these locations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining direct access to latestVersion that might not be protected
# We should only see accesses within methods that already hold the mutex

rg -A 3 "ndb\.latestVersion\s*[^=]*" --type go

Length of output: 4880


Script:

#!/bin/bash
# Check mutex lock/unlock patterns around these accesses
# Looking for mtx.Lock() calls before these accesses

# First, let's check nodedb.go for mutex patterns
rg -B 5 "mtx\.(Lock|Unlock)" nodedb.go

# Then, check if the test file has any mutex protection
rg -B 5 "mtx\.(Lock|Unlock)" nodedb_test.go

# Finally, check mutable_tree.go for the context of that access
rg -B 10 "version == tree\.ndb\.latestVersion" mutable_tree.go

Length of output: 6644

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, interesting, @cool-develope should I do as suggested here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, will check quickly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importer is OK since it is a fresh tree, I think mutable_tree.GetVersioned should be updated with getLatestVersion.

@@ -193,7 +193,7 @@ func (t *ImmutableTree) Get(key []byte) ([]byte, error) {
// 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 == t.ndb.safeGetLatestVersion() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use t.ndb.getLatestVersion() (thread safe)

Comment on lines +800 to +808
// 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
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Importer is OK since it is a fresh tree, I think mutable_tree.GetVersioned should be updated with getLatestVersion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants