-
Notifications
You must be signed in to change notification settings - Fork 20.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eth/catalyst: move block commit into its own go-routine to avoid deadlock #29657
Changes from 5 commits
1f14c9e
d4acec3
77bea8a
bd39709
a52979d
d0e9416
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package catalyst | |
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
@@ -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) | ||
newTxs = make(chan core.NewTxsEvent) | ||
sub = a.sim.eth.TxPool().SubscribeTransactions(newTxs, true) | ||
commitMu = sync.Mutex{} | ||
) | ||
defer sub.Unsubscribe() | ||
|
||
|
@@ -42,12 +44,36 @@ func (a *api) loop() { | |
case <-a.sim.shutdownCh: | ||
return | ||
case w := <-a.sim.withdrawals.pending: | ||
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) | ||
} | ||
go func() { | ||
commitMu.Lock() | ||
defer commitMu.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it matters, but this construction with the lock here will not preserve the receive order of select clauses. If both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be fine because eventually each go-routine will unblock and commit, making sure every pending new txs/withdrawals notification will trigger a commit. |
||
// When the beacon chain is ran by a simulator, then transaction insertion, | ||
// block insertion and block production will happen without any timing | ||
// delay between them. This will cause flaky simulator executions due to | ||
// the transaction pool running its internal reset operation on a back- | ||
// ground thread. To avoid the racey behavior - in simulator mode - the | ||
// pool will be explicitly blocked on its reset before continuing to the | ||
// block production below. | ||
if err := a.sim.eth.TxPool().Sync(); err != nil { | ||
log.Error("Failed to sync transaction pool", "err", err) | ||
return | ||
} | ||
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() { | ||
commitMu.Lock() | ||
defer commitMu.Unlock() | ||
|
||
if err := a.sim.eth.TxPool().Sync(); err != nil { | ||
log.Error("Failed to sync transaction pool", "err", err) | ||
return | ||
} | ||
a.sim.Commit() | ||
}() | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just fyi, the canonical way to initialize a variable with its zero value is just to declare it, i.e. you can write
Initializing with a zero literal is bad style.