fix: add txlookup lock to prevent reorg race and deadlock (ethereum#29343, #34039) - #127
Merged
Merged
Conversation
This change adds a lock to the transaction lookup cache, to avoid the case where reorgs make the lookup return inconsistent results. (cherry picked from commit 1126c6d8a57f1b7d9af0b39ac52f6eeb435f66f9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ports ethereum#29343 and ethereum#34039. During reorg, a concurrent
GetTransactionLookup()call could read stale DB data betweenPurge()and the new chain write, caching a wrong block hash/number. Additionally, #29343 introducedtxLookupLockwithoutdefer, leaving early-error return paths inreorg()unlocked and causing permanent deadlock.Upstream References
Problem
Race condition (#29343):
Purge()was called before writing new chain data. A concurrent RPC call hittingGetTransactionLookup()during this window read stale reorged data from DB and cached it, causingeth_getTransactionReceipt/eth_getTransactionto return wrong block hash and number until node restart.Deadlock (#34039):
reorg()acquiredtxLookupLock.Lock()but released it only on the happy path. Early returns onerrInvalidOldChain/errInvalidNewChainleaked the lock, permanently deadlocking all future transaction lookups and reorg attempts.Changes
core/blockchain.go: addtxLookupLock sync.RWMutextoBlockChain; inreorg(), acquire lock before mutation and usedefer Unlock()to protect all return paths; movePurge()to after DB writes completecore/blockchain_reader.go: addtxLookupLock.RLock()/defer RUnlock()inGetTransactionLookup()to block reads during reorg