Skip to content

Commit

Permalink
Fix deadlock in SimulatedBeacon.loop
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitagashkov-exness committed Apr 6, 2024
1 parent 7aafad2 commit 3349ba7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions eth/catalyst/simulated_beacon_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package catalyst

import (
"context"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -32,8 +33,9 @@ type api struct {

func (a *api) loop() {
var (
newTxs = make(chan core.NewTxsEvent)
sub = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true)
committing atomic.Bool
newTxs = make(chan core.NewTxsEvent)
sub = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true)
)
defer sub.Unsubscribe()

Expand All @@ -42,12 +44,21 @@ func (a *api) loop() {
case <-a.sim.shutdownCh:
return
case w := <-a.sim.withdrawals.pending:
// FIXME: `sealBlock` will block the loop and `newTxs` won't be
// read. If new TX will be submitted to the pool while we're
// sealing the block, deadlock will occur.
withdrawals := append(a.sim.withdrawals.gatherPending(9), w)
if err := a.sim.sealBlock(withdrawals, uint64(time.Now().Unix())); err != nil {
log.Warn("Error performing sealing work", "err", err)
}
case <-newTxs:
a.sim.Commit()
go func() {
if committing.Swap(true) {
return
}
a.sim.Commit()
committing.Store(false)
}()
}
}
}
Expand Down

0 comments on commit 3349ba7

Please sign in to comment.