Introduce fallibleCommit which can error

This allows the inner loop in the case of "on-demand" commits to bail out if the
txPool has been terminated before the doCommit channel is closed.

This has been tested with a known-flaky test which, before this commit was
spinlooping and logging unfathomable reams of warning messages. Now, when the
race occurs, only a single instance of the warning is logged.
This commit is contained in:
Pepper Lebeck-Jobe 2025-03-07 13:13:48 +01:00
parent 8d2920bbc4
commit 7fab62da4d
No known key found for this signature in database
2 changed files with 16 additions and 5 deletions

View file

@ -302,11 +302,18 @@ func (c *SimulatedBeacon) setCurrentState(headHash, finalizedHash common.Hash) {
// Commit seals a block on demand.
func (c *SimulatedBeacon) Commit() common.Hash {
hash, _ := c.fallibleCommit()
return hash
}
// fallibleCommit attempts to seal a block on demand, but may return an error.
func (c *SimulatedBeacon) fallibleCommit() (common.Hash, error) {
withdrawals := c.withdrawals.pop(10)
if err := c.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil {
log.Warn("Error performing sealing work", "err", err)
return common.Hash{}, err
}
return c.eth.BlockChain().CurrentBlock().Hash()
return c.eth.BlockChain().CurrentBlock().Hash(), nil
}
// Rollback un-sends previously added transactions.

View file

@ -70,11 +70,15 @@ func (a *simulatedBeaconAPI) loop() {
if executable, _ := a.sim.eth.TxPool().Stats(); executable == 0 {
break
}
a.sim.Commit()
// Avoid a race condition where tx pool is terminated during Commit.
if err := a.sim.eth.TxPool().Sync(); err != nil {
break
// Avoid a race condition where doCommit is closed while in this loop.
select {
case _, ok := <-doCommit:
if !ok {
break
}
default:
}
a.sim.Commit()
}
}
}()