Skip to content

fix: avoid mutating persisted nodes during clone - #1166

Open
rit3sh-x wants to merge 1 commit into
cosmos:masterfrom
rit3sh-x:fix/iavl-exporter-data-race
Open

rit3sh-x wants to merge 1 commit into
cosmos:masterfrom
rit3sh-x:fix/iavl-exporter-data-race

Conversation

@rit3sh-x

@rit3sh-x rit3sh-x commented Oct 8, 2026

Copy link
Copy Markdown

Summary

Fixes a data race between IAVL's exporter and Node.clone when the node cache is enabled.

A persisted node can be shared by an ImmutableTree and a concurrently-mutating MutableTree. Node.clone previously cleared the persisted node's leftNode and rightNode pointers after resolving them from the tree.

This mutates a node that may simultaneously be read by an exporter, causing a race under -race.

Changes

  • Remove the two leftNode = nil / rightNode = nil writes from Node.clone.
  • Add a comment documenting why persisted nodes must not be mutated during cloning.
  • Add TestExporter_ConcurrentSet, which reproduces the Cosmos SDK state-sync pattern:
    • Save version N.
    • Create an immutable tree for version N.
    • Start an exporter for version N.
    • Write version N+1 using the same mutable tree/cache.
    • Import the exported version into a fresh tree.
    • Verify the imported hash matches version N.

Validation

  • go test -short -race ./...: passed
  • go test ./...: passed
  • GOARCH=386 go test ./...: passed
  • TestExporter_ConcurrentSet: passed across 50 runs with -race

@rit3sh-x
rit3sh-x requested a review from a team as a code owner October 8, 2026 16:56
@greptile-apps

greptile-apps Bot commented Oct 8, 2026

Copy link
Copy Markdown

This PR does not match any of the 1 configured review trigger rule.

@rit3sh-x

rit3sh-x commented Oct 8, 2026

Copy link
Copy Markdown
Author

@cloudhead could you review it?

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.

1 participant