Repository navigation
Closed
Description
Bug Report
What happened?
There is a data race to access the variable recheckCursor in CListMempool on a particular recheck scenario when connected to the app via sockets.
This is the scenario:
- At the end of a mempool
Update, all txs in the mempool are sent to the app for rechecking their validity. So, for each tx one CheckTx request is sent to the app. Each request is sent in the order in which the txs appear in the mempool list. - Because we are using a socket connection, CheckTx responses may arrive in a different order in which the requests were sent.
- If a tx that is being rechecked is received again for validation, it may occur a data race when accessing
recheckCursor, which is used to traverseclistin order.
Related to tendermint/tendermint#5519.
How to reproduce it
Running the following test should produce a data race. This test was originally written for #1116, which includes a potential fix.
func TestMempoolRecheckRace(t *testing.T) {
sockPath := fmt.Sprintf("unix:///tmp/echo_%v.sock", cmtrand.Str(6))
app := kvstore.NewInMemoryApplication()
_, server := newRemoteApp(t, sockPath, app)
t.Cleanup(func() {
if err := server.Stop(); err != nil {
t.Error(err)
}
})
cfg := test.ResetTestRoot("mempool_test")
cfg.Mempool.Size = 1000
cfg.Mempool.CacheSize = 1000
mp, cleanup := newMempoolWithAppAndConfig(proxy.NewRemoteClientCreator(sockPath, "socket", true), cfg)
defer cleanup()
// Add a bunch of transactions to the mempool.
var err error
txs := newUniqueTxs(100)
for _, tx := range txs {
err = mp.CheckTx(tx, nil, TxInfo{})
require.NoError(t, err)
}
err = mp.FlushAppConn()
require.NoError(t, err)
// Update one transaction to force rechecking the rest.
mp.Lock()
err = mp.Update(1, txs[:1], abciResponses(1, abci.CodeTypeOK), nil, nil)
mp.Unlock()
require.NoError(t, err)
// Add again the same transaction that was updated. Checking this
// transaction and rechecking the others simultaneously should not result
// in a data race for the variable recheckCursor.
err = mp.CheckTx(txs[:1][0], nil, TxInfo{})
require.Equal(t, err, ErrTxInCache)
}
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsTodo
- StatusShow more project fieldsDone
Activity
cason commentedon Apr 17, 2024
I couldn't reproduce the race condition. There are errors in the test.
By using the version of this test introduced by #2268 I couldn't reproduce this race condition either. I've run with the
-raceflag.hvanz commentedon Apr 25, 2024
I also couldn't reproduce the error in main (commit b139e13) when running the test. However, the test still fails on v0.38.x (f6cde44). I need to investigate what's the difference.
fix(mempool): Fix data race when rechecking with async ABCI client (#…
fix(mempool): Fix data race when rechecking with async ABCI client (#…
fix(mempool): Fix data race when rechecking with async ABCI client (#…
fix(mempool): Fix data race when rechecking with async ABCI client (b…
fix(mempool): Fix data race when rechecking with async ABCI client (b…
ValarDragon commentedon May 23, 2024
Doesn't this fix introduce potential O(N^2) behavior now? Or is this fine for in-process mempools, since the callbacks come back in order?
hvanz commentedon May 23, 2024
The fix shouldn't change the recheck logic and, in particular for in-process applications, the behaviour should be the same. This is a corner case on non-local clients that happens when ReCheckTx responses are dropped. What's the O(N^2) behaviour you have in mind?
ValarDragon commentedon May 23, 2024
I was worried that every callback may have to do an O(N) scan of the mempool, but this is only likely if the the app returns txs in ~random orders, which is unlikely to happen
hvanz commentedon May 24, 2024
Right, the callback only advances the recheck cursor until reaching the end of the list. So it's traversed only once.