From 4497748e146f22069056be398738e4b656149284 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Sat, 18 Jan 2025 17:28:42 +0700 Subject: [PATCH] pool: race in logging, part2 (#13487) ``` ================== WARNING: DATA RACE Write at 0x00c029988f40 by goroutine 322: github.com/erigontech/erigon/txnprovider/txpool.(*bestSlice).UnsafeAdd() /home/ubuntu/erigon/txnprovider/txpool/queues.go:50 +0x444 github.com/erigontech/erigon/txnprovider/txpool.(*PendingPool).Add() /home/ubuntu/erigon/txnprovider/txpool/pending_pool.go:97 +0x325 github.com/erigontech/erigon/txnprovider/txpool.(*TxPool).promote() /home/ubuntu/erigon/txnprovider/txpool/pool.go:1819 +0x194d github.com/erigontech/erigon/txnprovider/txpool.(*TxPool).addTxns() /home/ubuntu/erigon/txnprovider/txpool/pool.go:1327 +0xd84 github.com/erigontech/erigon/txnprovider/txpool.(*TxPool).processRemoteTxns() /home/ubuntu/erigon/txnprovider/txpool/pool.go:511 +0x7c7 github.com/erigontech/erigon/txnprovider/txpool.(*TxPool).Run() /home/ubuntu/erigon/txnprovider/txpool/pool.go:1887 +0x9b2 github.com/erigontech/erigon/eth.(*Ethereum).Start.func3() /home/ubuntu/erigon/eth/backend.go:1594 +0x7a golang.org/x/sync/errgroup.(*Group).Go.func1() /home/ubuntu/go/pkg/mod/golang.org/x/sync@v0.10.0/errgroup/errgroup.go:78 +0x91 Previous read at 0x00c029988f40 by goroutine 324: github.com/erigontech/erigon/txnprovider/txpool.(*TxPool).OnNewBlock() /home/ubuntu/erigon/txnprovider/txpool/pool.go:310 +0x3c4 github.com/erigontech/erigon/txnprovider/txpool.(*Fetch).handleStateChangesRequest() /home/ubuntu/erigon/txnprovider/txpool/fetch.go:516 +0x47a github.com/erigontech/erigon/txnprovider/txpool.(*Fetch).handleStateChanges() /home/ubuntu/erigon/txnprovider/txpool/fetch.go:468 +0x2ad github.com/erigontech/erigon/txnprovider/txpool.(*Fetch).ConnectCore.func1() /home/ubuntu/erigon/txnprovider/txpool/fetch.go:124 +0xa4 ``` also renamed `coreDB` to `chainDB` --- txnprovider/txpool/pool.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/txnprovider/txpool/pool.go b/txnprovider/txpool/pool.go index b0546d5c2c2..b6d6b5317b9 100644 --- a/txnprovider/txpool/pool.go +++ b/txnprovider/txpool/pool.go @@ -162,7 +162,7 @@ func New( ctx context.Context, newTxns chan Announcements, poolDB kv.RwDB, - coreDB kv.RoDB, + chainDB kv.RoDB, cfg txpoolcfg.Config, cache kvcache.Cache, chainID uint256.Int, @@ -217,7 +217,7 @@ func New( _stateCache: cache, senders: newSendersBatch(tracedSenders), poolDB: poolDB, - _chainDB: coreDB, + _chainDB: chainDB, cfg: cfg, chainID: chainID, unprocessedRemoteTxns: &TxnSlots{}, @@ -273,7 +273,7 @@ func (p *TxPool) start(ctx context.Context) error { } return p.poolDB.View(ctx, func(tx kv.Tx) error { - coreDb, _ := p.coreDBWithCache() + coreDb, _ := p.chainDB() coreTx, err := coreDb.BeginRo(ctx) if err != nil { return err @@ -296,7 +296,7 @@ func (p *TxPool) start(ctx context.Context) error { func (p *TxPool) OnNewBlock(ctx context.Context, stateChanges *remote.StateChangeBatch, unwindTxns, unwindBlobTxns, minedTxns TxnSlots) error { defer newBlockTimer.ObserveDuration(time.Now()) - coreDB, cache := p.coreDBWithCache() + coreDB, cache := p.chainDB() cache.OnNewBlock(stateChanges) coreTx, err := coreDB.BeginRo(ctx) if err != nil { @@ -307,7 +307,6 @@ func (p *TxPool) OnNewBlock(ctx context.Context, stateChanges *remote.StateChang block := stateChanges.ChangeBatch[len(stateChanges.ChangeBatch)-1].BlockHeight baseFee := stateChanges.PendingBlockBaseFee - available := len(p.pending.best.ms) if err = minedTxns.Valid(); err != nil { return err @@ -329,6 +328,7 @@ func (p *TxPool) OnNewBlock(ctx context.Context, stateChanges *remote.StateChang }() defer func() { + available := len(p.pending.best.ms) p.logger.Debug("[txpool] New block", "block", block, "unwound", len(unwindTxns.Txns), "mined", len(minedTxns.Txns), "baseFee", baseFee, "pending-pre", available, "pending", p.pending.Len(), "baseFee", p.baseFee.Len(), "queued", p.queued.Len(), "err", err) }() @@ -479,7 +479,7 @@ func (p *TxPool) processRemoteTxns(ctx context.Context) (err error) { } defer processBatchTxnsTimer.ObserveDuration(time.Now()) - coreDB, cache := p.coreDBWithCache() + coreDB, cache := p.chainDB() coreTx, err := coreDB.BeginRo(ctx) if err != nil { return err @@ -1209,7 +1209,7 @@ func fillDiscardReasons(reasons []txpoolcfg.DiscardReason, newTxns TxnSlots, dis } func (p *TxPool) AddLocalTxns(ctx context.Context, newTxns TxnSlots) ([]txpoolcfg.DiscardReason, error) { - coreDb, cache := p.coreDBWithCache() + coreDb, cache := p.chainDB() coreTx, err := coreDb.BeginRo(ctx) if err != nil { return nil, err @@ -1266,7 +1266,7 @@ func (p *TxPool) AddLocalTxns(ctx context.Context, newTxns TxnSlots) ([]txpoolcf return reasons, nil } -func (p *TxPool) coreDBWithCache() (kv.RoDB, kvcache.Cache) { +func (p *TxPool) chainDB() (kv.RoDB, kvcache.Cache) { p.lock.Lock() defer p.lock.Unlock() return p._chainDB, p._stateCache