Skip to content

mempool: Data race when rechecking with socket connection #1827

Closed

Description

@hvanz

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 traverse clist in 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)
}

Activity

  1. added
    bug
    on Dec 14, 2023
  2. added this to the 2024-Q2 milestone on Apr 3, 2024
  3. cason commented on Apr 17, 2024

    @cason

    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 -race flag.

  4. hvanz commented on Apr 25, 2024

    @hvanz
    CollaboratorAuthor

    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.

  5. moved this from Todo to Done in CometBFTon May 7, 2024
  6. ValarDragon commented on May 23, 2024

    @ValarDragon
    Contributor

    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?

  7. hvanz commented on May 23, 2024

    @hvanz
    CollaboratorAuthor

    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?

  8. ValarDragon commented on May 23, 2024

    @ValarDragon
    Contributor

    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

  9. hvanz commented on May 24, 2024

    @hvanz
    CollaboratorAuthor

    Right, the callback only advances the recheck cursor until reaching the end of the list. So it's traversed only once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmempool

    Type

    No type

    Projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions