This repository was archived by the owner on Jan 20, 2026. It is now read-only.
[SeiDB] Fix MemIAVL Race Condition during snapshot reload#56
Merged
Conversation
yihuang
reviewed
Jan 30, 2024
yihuang
reviewed
Jan 31, 2024
| } | ||
|
|
||
| func (t *Tree) Iterator(start, end []byte, ascending bool) dbm.Iterator { | ||
| t.mtx.RLock() |
There was a problem hiding this comment.
Iterator is tricker, it supposed the hold the RLock before the iterator is closed
Collaborator
Author
There was a problem hiding this comment.
Hmm yeah that's a good point, iterator is a bit tricky, this lock only covers the creation of the iterator, might not be enough. I think for now it's probably fine since we don't have a lot of use cases using iterators in EVM, we definitely can add more locking coverage in the future
Kbhat1
approved these changes
Jan 31, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes and provide context
Problem:
In EVM, serving EVM RPC endpoints will have to query and access the latest state or historical state, when accessing latest state from MemIAVL, there's a race condition where the key we are trying to access doesn't exist because in the background, we have already switched to a difference snapshot after snapshot rewrite completes. Here's how it could happen:
And here's the error when this race condition is triggered:

Solution:
This PR address the issue by doing an internal replacement, instead of closing the old tree and create a new tree, we replace all the variables within each tree in a thread-safe manner as well as clean up and closing the old snapshots safely, this guarantees that all other operations will be block while we are replacing the whole tree.
Testing performed to validate your change
Add Unit Test to repro and catch race conditions