Skip to content

fix: memiavl-nil-deref-during-state-sync#3518

Open
blindchaser wants to merge 1 commit into
mainfrom
yiren/fix-memiavl-nil-deref-during-state-sync
Open

fix: memiavl-nil-deref-during-state-sync#3518
blindchaser wants to merge 1 commit into
mainfrom
yiren/fix-memiavl-nil-deref-during-state-sync

Conversation

@blindchaser
Copy link
Copy Markdown
Contributor

@blindchaser blindchaser commented May 28, 2026

Summary

During state-sync the mempool reactor runs CheckTx (→ GetConsensusParamsRouterCommitKVStore.HasRouter.Read) before memiavl's LoadVersion opens the underlying *DB. With cs.db == nil this nil-derefs and crashes the node. Affects any node still backed by memiavl (default MemiavlOnly included), not just EVM-migration nodes.

This makes the memiavl read path safe during that pre-load window:

  • memiavl/db.go: TreeByName returns nil on a nil receiver instead of locking db.mtx.
  • memiavl/store.go: GetChildStoreByName returns a nil interface when unloaded; adds IsLoaded().
  • migration/router_builder.go: memiavl closures gate on IsLoaded(). Reads return key-not-found (no error), so Has/Get no longer panic. Writes/iterators/proofs return an explicit error rather than corrupting state or faking a proof.

Scope / known limits:

  • Only the read path is fully panic-free. Iterator/GetProof/Set still surface as a panic at RouterCommitKVStore (now with a clear "not loaded yet" message instead of a nil deref).
  • cs.db is still accessed without synchronization vs. LoadVersion; this fixes the deterministic nil-deref, not a potential data race.
  • Defensive fix at the storage layer; does not change whether the reactor dispatches CheckTx during state-sync.

Test plan

  • memiavl/unloaded_store_test.go: IsLoaded transitions; nil-safe GetChildStoreByName/TreeByName before load.
  • migration/router_builder_unloaded_test.go: reader returns not-found without error; writer/iterator/proof error before load; TestModuleRouter_Read_BeforeLoad_DoesNotPanic reproduces the original panic stack.

@cursor
Copy link
Copy Markdown

cursor Bot commented May 28, 2026

PR Summary

Medium Risk
Touches consensus-adjacent read paths during state-sync (params/CheckTx); behavior change is intentional (not-found vs panic) but must not mask real misconfiguration after load.

Overview
Prevents crashes when CheckTx or other reads hit memIAVL before LoadVersion finishes during state-sync (mempool can run while the snapshot is still applying).

memiavl now treats an unopened store as safe nil: DB.TreeByName and CommitStore.GetChildStoreByName no longer dereference a nil *DB, and CommitStore.IsLoaded() exposes whether the DB is open. The migration router uses that to return key-not-found on reads in the pre-load window, while writes, iteration, and proofs fail with explicit errors instead of silent corruption.

New tests lock in nil-receiver behavior and the router contract on an unloaded CommitStore.

Reviewed by Cursor Bugbot for commit 1c80d55. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedMay 28, 2026, 9:06 PM

@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.65%. Comparing base (8567422) to head (1c80d55).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3518      +/-   ##
==========================================
- Coverage   59.03%   58.65%   -0.38%     
==========================================
  Files        2199     2159      -40     
  Lines      182207   178092    -4115     
==========================================
- Hits       107569   104468    -3101     
+ Misses      64975    64315     -660     
+ Partials     9663     9309     -354     
Flag Coverage Δ
sei-db 70.41% <ø> (ø)
sei-db-state-db ?
sei-db-state-db-pr 77.79% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sei-db/state_db/sc/memiavl/db.go 66.76% <100.00%> (+0.09%) ⬆️
sei-db/state_db/sc/memiavl/store.go 92.80% <100.00%> (+0.21%) ⬆️
sei-db/state_db/sc/migration/router_builder.go 57.82% <100.00%> (+1.10%) ⬆️

... and 41 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@blindchaser blindchaser changed the title fix: fix-memiavl-nil-deref-during-state-sync fix: memiavl-nil-deref-during-state-sync May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant