Skip to content

fix(mempool): guard concurrent access to the tx index - #1341

Merged
lklimek merged 1 commit into
v1.6-devfrom
fix/mempool-concurrent-read
Jun 15, 2026
Merged

lklimek merged 1 commit into
v1.6-devfrom
fix/mempool-concurrent-read

Conversation

@lklimek

@lklimek lklimek commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Why this PR exists

  • Problem: Mempool.GetTxByHash reads the txByKey map without holding the mempool mutex (mtx), while every other accessor of that map is synchronized via mtx (see the "Synchronized fields, protected by mtx" struct comment in mempool.go).
  • What breaks without it: A read concurrent with a writer is a data race, and the Go runtime promotes a detected map data race to a fatal, unrecoverable error — the process crashes. The unlocked read is reachable from the UnconfirmedTx RPC handler, so a client RPC racing normal mempool churn can crash the node (availability / DoS).
  • Blocking relationship: Independent hardening fix; one of a batch of v1.5-dev robustness/security fixes. Not stacked on any other PR.

What was done

  • Take the read lock (mtx.RLock) around the txByKey lookup in GetTxByHash, matching the existing pattern in allEntriesSorted.
  • The only production caller (UnconfirmedTx RPC handler) holds no mempool lock, and the returned clist.CElement.Value is immutable, so no nested-lock / lock-ordering path is introduced.

Testing

  • Added a table-driven unit test for GetTxByHash.
  • Added a concurrent regression test that fails under the race detector (go test -race) without the fix and passes with it.

Breaking changes

None.

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation (n/a — internal concurrency fix)

Attribution

🤖 Co-authored by Claudius the Magnificent AI Agent

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]>
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ef7d6d9c-ba2d-49cd-8145-163f81b2fd7d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mempool-concurrent-read

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lklimek
lklimek requested a review from Copilot June 15, 2026 09:44
@lklimek
lklimek marked this pull request as ready for review June 15, 2026 09:46
@lklimek
lklimek requested a review from QuantumExplorer as a code owner June 15, 2026 09:46
@thepastaclaw

thepastaclaw commented Jun 15, 2026 •

Copy link
Copy Markdown

✅ Review complete (commit e0a4410)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/RUnlock around the txByKey lookup in GetTxByHash.
  • Add a table-driven unit test covering present/absent lookups.
  • Add a concurrent test that stresses GetTxByHash against concurrent inserts/deletes (intended for go 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.

Comment thread internal/mempool/mempool_test.go Resolved
Comment on lines +904 to +907
key := tx.tx.Key()
keys.Store(key, struct{}{})
_ = txmp.RemoveTxByKey(key)
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thepastaclaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lklimek
lklimek changed the base branch from v1.5-dev to v1.6-dev June 15, 2026 10:57
@lklimek
lklimek merged commit cbe4f43 into v1.6-dev Jun 15, 2026
16 checks passed
@lklimek
lklimek deleted the fix/mempool-concurrent-read branch June 15, 2026 10:59
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.

3 participants