Skip to content

Commit b161df0

Browse files
committed
Use write-lock in (*TxPriorityQueue).ReapMax funcs
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).
1 parent a84cd86 commit b161df0

File tree

2 files changed

+66
-34
lines changed

2 files changed

+66
-34
lines changed

internal/mempool/mempool.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ func (txmp *TxMempool) Flush() {
362362
// - Transactions returned are not removed from the mempool transaction
363363
// store or indexes.
364364
func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
365-
txmp.mtx.RLock()
366-
defer txmp.mtx.RUnlock()
365+
txmp.mtx.Lock()
366+
defer txmp.mtx.Unlock()
367367

368368
var (
369369
totalGas int64
@@ -417,8 +417,8 @@ func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
417417
// - Transactions returned are not removed from the mempool transaction
418418
// store or indexes.
419419
func (txmp *TxMempool) ReapMaxTxs(max int) types.Txs {
420-
txmp.mtx.RLock()
421-
defer txmp.mtx.RUnlock()
420+
txmp.mtx.Lock()
421+
defer txmp.mtx.Unlock()
422422

423423
numTxs := txmp.priorityIndex.NumTxs()
424424
if max < 0 {

internal/mempool/mempool_test.go

+62-30
Original file line numberDiff line numberDiff line change
@@ -305,27 +305,43 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) {
305305
require.Equal(t, priorities[:len(reapedPriorities)], reapedPriorities)
306306
}
307307

308+
var wg sync.WaitGroup
309+
308310
// reap by gas capacity only
309-
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50)
310-
ensurePrioritized(reapedTxs)
311-
require.Equal(t, len(tTxs), txmp.Size())
312-
require.Equal(t, int64(5690), txmp.SizeBytes())
313-
require.Len(t, reapedTxs, 50)
311+
wg.Add(1)
312+
go func() {
313+
defer wg.Done()
314+
reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50)
315+
ensurePrioritized(reapedTxs)
316+
require.Equal(t, len(tTxs), txmp.Size())
317+
require.Equal(t, int64(5690), txmp.SizeBytes())
318+
require.Len(t, reapedTxs, 50)
319+
}()
314320

315321
// reap by transaction bytes only
316-
reapedTxs = txmp.ReapMaxBytesMaxGas(1000, -1)
317-
ensurePrioritized(reapedTxs)
318-
require.Equal(t, len(tTxs), txmp.Size())
319-
require.Equal(t, int64(5690), txmp.SizeBytes())
320-
require.GreaterOrEqual(t, len(reapedTxs), 16)
322+
wg.Add(1)
323+
go func() {
324+
defer wg.Done()
325+
reapedTxs := txmp.ReapMaxBytesMaxGas(1000, -1)
326+
ensurePrioritized(reapedTxs)
327+
require.Equal(t, len(tTxs), txmp.Size())
328+
require.Equal(t, int64(5690), txmp.SizeBytes())
329+
require.GreaterOrEqual(t, len(reapedTxs), 16)
330+
}()
321331

322332
// Reap by both transaction bytes and gas, where the size yields 31 reaped
323333
// transactions and the gas limit reaps 25 transactions.
324-
reapedTxs = txmp.ReapMaxBytesMaxGas(1500, 30)
325-
ensurePrioritized(reapedTxs)
326-
require.Equal(t, len(tTxs), txmp.Size())
327-
require.Equal(t, int64(5690), txmp.SizeBytes())
328-
require.Len(t, reapedTxs, 25)
334+
wg.Add(1)
335+
go func() {
336+
defer wg.Done()
337+
reapedTxs := txmp.ReapMaxBytesMaxGas(1500, 30)
338+
ensurePrioritized(reapedTxs)
339+
require.Equal(t, len(tTxs), txmp.Size())
340+
require.Equal(t, int64(5690), txmp.SizeBytes())
341+
require.Len(t, reapedTxs, 25)
342+
}()
343+
344+
wg.Wait()
329345
}
330346

331347
func TestTxMempool_ReapMaxTxs(t *testing.T) {
@@ -364,26 +380,42 @@ func TestTxMempool_ReapMaxTxs(t *testing.T) {
364380
require.Equal(t, priorities[:len(reapedPriorities)], reapedPriorities)
365381
}
366382

383+
var wg sync.WaitGroup
384+
367385
// reap all transactions
368-
reapedTxs := txmp.ReapMaxTxs(-1)
369-
ensurePrioritized(reapedTxs)
370-
require.Equal(t, len(tTxs), txmp.Size())
371-
require.Equal(t, int64(5690), txmp.SizeBytes())
372-
require.Len(t, reapedTxs, len(tTxs))
386+
wg.Add(1)
387+
go func() {
388+
defer wg.Done()
389+
reapedTxs := txmp.ReapMaxTxs(-1)
390+
ensurePrioritized(reapedTxs)
391+
require.Equal(t, len(tTxs), txmp.Size())
392+
require.Equal(t, int64(5690), txmp.SizeBytes())
393+
require.Len(t, reapedTxs, len(tTxs))
394+
}()
373395

374396
// reap a single transaction
375-
reapedTxs = txmp.ReapMaxTxs(1)
376-
ensurePrioritized(reapedTxs)
377-
require.Equal(t, len(tTxs), txmp.Size())
378-
require.Equal(t, int64(5690), txmp.SizeBytes())
379-
require.Len(t, reapedTxs, 1)
397+
wg.Add(1)
398+
go func() {
399+
defer wg.Done()
400+
reapedTxs := txmp.ReapMaxTxs(1)
401+
ensurePrioritized(reapedTxs)
402+
require.Equal(t, len(tTxs), txmp.Size())
403+
require.Equal(t, int64(5690), txmp.SizeBytes())
404+
require.Len(t, reapedTxs, 1)
405+
}()
380406

381407
// reap half of the transactions
382-
reapedTxs = txmp.ReapMaxTxs(len(tTxs) / 2)
383-
ensurePrioritized(reapedTxs)
384-
require.Equal(t, len(tTxs), txmp.Size())
385-
require.Equal(t, int64(5690), txmp.SizeBytes())
386-
require.Len(t, reapedTxs, len(tTxs)/2)
408+
wg.Add(1)
409+
go func() {
410+
defer wg.Done()
411+
reapedTxs := txmp.ReapMaxTxs(len(tTxs) / 2)
412+
ensurePrioritized(reapedTxs)
413+
require.Equal(t, len(tTxs), txmp.Size())
414+
require.Equal(t, int64(5690), txmp.SizeBytes())
415+
require.Len(t, reapedTxs, len(tTxs)/2)
416+
}()
417+
418+
wg.Wait()
387419
}
388420

389421
func TestTxMempool_CheckTxExceedsMaxSize(t *testing.T) {

0 commit comments

Comments
 (0)