Skip to content

Commit

Permalink
eth/gasprice: ensure cache purging goroutine terminates with subscrip…
Browse files Browse the repository at this point in the history
…tion (#31025)
  • Loading branch information
jwasinger authored Jan 14, 2025
1 parent 04a336a commit 8dfad57
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,23 @@ func NewOracle(backend OracleBackend, params Config, startPrice *big.Int) *Oracl

cache := lru.NewCache[cacheKey, processedFees](2048)
headEvent := make(chan core.ChainHeadEvent, 1)
backend.SubscribeChainHeadEvent(headEvent)
go func() {
var lastHead common.Hash
for ev := range headEvent {
if ev.Header.ParentHash != lastHead {
cache.Purge()
sub := backend.SubscribeChainHeadEvent(headEvent)
if sub != nil { // the gasprice testBackend doesn't support subscribing to head events
go func() {
var lastHead common.Hash
for {
select {
case ev := <-headEvent:
if ev.Header.ParentHash != lastHead {
cache.Purge()
}
lastHead = ev.Header.Hash()
case <-sub.Err():
return
}
}
lastHead = ev.Header.Hash()
}
}()
}()
}

return &Oracle{
backend: backend,
Expand Down

0 comments on commit 8dfad57

Please sign in to comment.