Repository navigation
fix(mempool): guard concurrent access to the tx index - #1341
Conversation
GetTxByHash read txByKey without holding the mempool mutex, while every other accessor of that map is synchronized via mtx (see the "Synchronized fields, protected by mtx" struct comment in mempool.go). A read concurrent with a writer is a data race the Go runtime turns into a fatal error. Take the read lock around the lookup, matching allEntriesSorted. The only production caller (UnconfirmedTx RPC handler) holds no mempool lock and the returned clist.CElement.Value is immutable, so no nested-lock path is introduced. Adds a table-driven test and a concurrent regression test that fails the race detector without the fix. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
✅ Review complete (commit e0a4410) |
There was a problem hiding this comment.
Pull request overview
This PR hardens the mempool against a Go map data race by synchronizing TxMempool.GetTxByHash access to the txByKey index, and adds tests intended to catch the regression (including under -race).
Changes:
- Add
mtx.RLock/RUnlockaround thetxByKeylookup inGetTxByHash. - Add a table-driven unit test covering present/absent lookups.
- Add a concurrent test that stresses
GetTxByHashagainst concurrent inserts/deletes (intended forgo test -race).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
internal/mempool/mempool.go |
Adds a read lock to synchronize txByKey map access in GetTxByHash. |
internal/mempool/mempool_test.go |
Adds unit + concurrent regression tests for GetTxByHash. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| key := tx.tx.Key() | ||
| keys.Store(key, struct{}{}) | ||
| _ = txmp.RemoveTxByKey(key) | ||
| } |
There was a problem hiding this comment.
Valid suggestion (LOW), not yet addressed — the test currently does _ = txmp.RemoveTxByKey(key). One caveat for whoever implements it: this runs inside the spawned writer goroutine, so it must use assert.NoError (or funnel the error back to the test goroutine via a channel), NOT require.NoError — calling require/t.FailNow() from a non-test goroutine is itself illegal and would introduce a different bug. Leaving this thread open for a maintainer decision.
🤖 Co-authored by Claudius the Magnificent AI Agent
thepastaclaw
left a comment
There was a problem hiding this comment.
Code Review
Minimal, correct concurrency fix: GetTxByHash now takes the mempool RLock around the txByKey lookup, restoring the documented synchronization invariant. The returned tx slice is immutable post-construction and the sole production caller holds no mempool lock, so no lock-ordering or reentrancy risk is introduced. Tests cover normal lookup semantics and structure a race-detector regression case.
Why this PR exists
Mempool.GetTxByHashreads thetxByKeymap without holding the mempool mutex (mtx), while every other accessor of that map is synchronized viamtx(see the "Synchronized fields, protected by mtx" struct comment inmempool.go).UnconfirmedTxRPC handler, so a client RPC racing normal mempool churn can crash the node (availability / DoS).v1.5-devrobustness/security fixes. Not stacked on any other PR.What was done
mtx.RLock) around thetxByKeylookup inGetTxByHash, matching the existing pattern inallEntriesSorted.UnconfirmedTxRPC handler) holds no mempool lock, and the returnedclist.CElement.Valueis immutable, so no nested-lock / lock-ordering path is introduced.Testing
GetTxByHash.go test -race) without the fix and passes with it.Breaking changes
None.
Checklist
Attribution
🤖 Co-authored by Claudius the Magnificent AI Agent