crypto-org-chain / crypto-org-chain/cronos-store

memiavlstore.Store: queries can race ApplyChangeSet mutating the live *memiavl.Tree

Open
#104 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
0
Forks
3
PR merge metrics
No merged PRs in 30d

Description

### What

`memiavlstore.Store.tree` is an `atomic.Pointer[memiavl.Tree]`, swapped by `SetTree` after a commit builds the next version. That makes the pointer swap itself race-free, but it doesn't protect the tree object underneath.

### Issue

`rootmulti.Store.flush()` calls `rs.db.ApplyChangeSets(...)` (store/rootmulti/store.go), which mutates the current `*memiavl.Tree`'s internals (node cache, dirty tracking) in place, before `SetTree` publishes the new tree. A query goroutine on another ABCI connection can `Load()` the pre-commit tree pointer and call `Get`/`Has`/iterate on it while `ApplyChangeSet` is mutating the same object — a data race on the tree's internals, not on the `Store.tree` field.

### Repro sketch

- Goroutine A: `rootmulti.Store.Commit()` -> `flush()` -> `rs.db.ApplyChangeSets(...)` mutating tree T in place.
- Goroutine B (concurrent ABCI query on another connection): `memiavlstore.Store.Get(key)` -> `st.tree.Load()` returns T (not yet swapped) -> reads T's internals concurrently with A's mutation.

`go test -race` across a workload that mixes queries with commits should be able to surface this.

### Notes

Found while reviewing #103 (WAL durability fix) — noted in a comment at `store/memiavlstore/store.go` on the `tree` field, but out of scope for that PR. Filing separately to fix later.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with store/memiavlstore/store.go, especially the tree field and its Get/Has methods, then trace rootmulti.Store.flush() in store/rootmulti/store.go through ApplyChangeSets and SetTree. Run a mixed query/commit workload with go test -race; done means concurrent queries and commits no longer race on the live tree internals.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.