Skip to content

Commit 0922668

Browse files
stevenlanderssigv
authored andcommitted
Add heapIndex with safety check (#213)
* add heapIndex with safety check * cleanup * comment out for perf test * add back perf improvement * fix nil test * Use write-lock in (*TxPriorityQueue).ReapMax funcs (#209) ReapMaxBytesMaxGas and ReapMaxTxs funcs in TxPriorityQueue claim > Transactions returned are not removed from the mempool transaction > store or indexes. However, they use a priority queue to accomplish the claim > Transaction are retrieved in priority order. This is accomplished by popping all items out of the whole heap, and then pushing then back in sequentially. A copy of the heap cannot be obtained otherwise. Both of the mentioned functions use a read-lock (RLock) when doing this. This results in a potential scenario where multiple executions of the ReapMax can be started in parallel, and both would be popping items out of the priority queue. In practice, this can be abused by executing the `unconfirmed_txs` RPC call repeatedly. Based on our observations, running it multiple times per millisecond results in multiple threads picking it up at the same time. Such a scenario can be obtained via the WebSocket interface, and spamming `unconfirmed_txs` calls there. The behavior that happens is a `Panic in WSJSONRPC handler` when a queue item unexpectedly disappears for `mempool.(*TxPriorityQueue).Swap`. (`runtime error: index out of range [0] with length 0`) This can additionally lead to a `CONSENSUS FAILURE!!!` if the race condition occurs for `internal/consensus.(*State).finalizeCommit` when it tries to do `mempool.(*TxPriorityQueue).RemoveTx`, but the ReapMax has already removed all elements from the underlying heap. (`runtime error: index out of range [-1]`) This commit switches the lock type to a write-lock (Lock) to ensure no parallel modifications take place. This commit additionally updates the tests to allow parallel execution of the func calls in testing, as to prevent regressions (in case someone wants to downgrade the locks without considering the implications from the underlying heap usage). --------- Co-authored-by: Valters Jansons <sigv@users.noreply.github.com>
1 parent 14a366f commit 0922668

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

internal/mempool/priority_queue.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ func (pq *TxPriorityQueue) removeQueuedEvmTxUnsafe(tx *WrappedTx) (removedIdx in
178178
}
179179

180180
func (pq *TxPriorityQueue) findTxIndexUnsafe(tx *WrappedTx) (int, bool) {
181+
// safety check for race situation where heapIndex is out of range of txs
182+
if tx.heapIndex >= 0 && tx.heapIndex < len(pq.txs) && pq.txs[tx.heapIndex].tx.Key() == tx.tx.Key() {
183+
return tx.heapIndex, true
184+
}
185+
186+
// heap index isn't trustable here, so attempt to find it
181187
for i, t := range pq.txs {
182188
if t.tx.Key() == tx.tx.Key() {
183189
return i, true
@@ -443,7 +449,9 @@ func (pq *TxPriorityQueue) PeekTxs(max int) []*WrappedTx {
443449
//
444450
// NOTE: A caller should never call Push. Use PushTx instead.
445451
func (pq *TxPriorityQueue) Push(x interface{}) {
452+
n := len(pq.txs)
446453
item := x.(*WrappedTx)
454+
item.heapIndex = n
447455
pq.txs = append(pq.txs, item)
448456
}
449457

@@ -454,7 +462,8 @@ func (pq *TxPriorityQueue) Pop() interface{} {
454462
old := pq.txs
455463
n := len(old)
456464
item := old[n-1]
457-
old[n-1] = nil // avoid memory leak
465+
old[n-1] = nil // avoid memory leak
466+
setHeapIndex(item, -1) // for safety
458467
pq.txs = old[0 : n-1]
459468
return item
460469
}
@@ -483,4 +492,14 @@ func (pq *TxPriorityQueue) Less(i, j int) bool {
483492
// Swap implements the Heap interface. It swaps two transactions in the queue.
484493
func (pq *TxPriorityQueue) Swap(i, j int) {
485494
pq.txs[i], pq.txs[j] = pq.txs[j], pq.txs[i]
495+
setHeapIndex(pq.txs[i], i)
496+
setHeapIndex(pq.txs[j], j)
497+
}
498+
499+
func setHeapIndex(tx *WrappedTx, i int) {
500+
// a removed tx can be nil
501+
if tx == nil {
502+
return
503+
}
504+
tx.heapIndex = i
486505
}

0 commit comments

Comments
 (0)