core/txpool/locals: fix data race (#35096)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Keeper Build (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

Supersedes #35060

```
go test -race ./core/txpool/locals/
ok      github.com/ethereum/go-ethereum/core/txpool/locals      1.782s
```
This commit is contained in:
rjl493456442 2026-06-02 09:46:11 +08:00 committed by GitHub
parent fdf99d9883
commit 02dd66dfc0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -173,6 +173,17 @@ func (tracker *TxTracker) recheck(journalCheck bool) []*types.Transaction {
// Start is called after all services have been constructed and the networking // Start is called after all services have been constructed and the networking
// layer was also initialized to spawn any goroutines required by the service. // layer was also initialized to spawn any goroutines required by the service.
func (tracker *TxTracker) Start() error { func (tracker *TxTracker) Start() error {
if tracker.journal != nil {
tracker.journal.load(func(transactions []*types.Transaction) []error {
tracker.TrackAll(transactions)
return nil
})
// Setup the writer for the upcoming transactions
if err := tracker.journal.setupWriter(); err != nil {
log.Error("Failed to setup the journal writer", "err", err)
return err
}
}
tracker.wg.Add(1) tracker.wg.Add(1)
go tracker.loop() go tracker.loop()
return nil return nil
@ -184,25 +195,19 @@ func (tracker *TxTracker) Start() error {
func (tracker *TxTracker) Stop() error { func (tracker *TxTracker) Stop() error {
close(tracker.shutdownCh) close(tracker.shutdownCh)
tracker.wg.Wait() tracker.wg.Wait()
return nil
tracker.mu.Lock()
var err error
if tracker.journal != nil {
err = tracker.journal.close()
}
tracker.mu.Unlock()
return err
} }
func (tracker *TxTracker) loop() { func (tracker *TxTracker) loop() {
defer tracker.wg.Done() defer tracker.wg.Done()
if tracker.journal != nil {
tracker.journal.load(func(transactions []*types.Transaction) []error {
tracker.TrackAll(transactions)
return nil
})
// Setup the writer for the upcoming transactions
if err := tracker.journal.setupWriter(); err != nil {
log.Error("Failed to setup the journal writer", "err", err)
return
}
defer tracker.journal.close()
}
var ( var (
lastJournal = time.Now() lastJournal = time.Now()
timer = time.NewTimer(10 * time.Second) // Do initial check after 10 seconds, do rechecks more seldom. timer = time.NewTimer(10 * time.Second) // Do initial check after 10 seconds, do rechecks more seldom.