eth/gasprice: ensure cache purging goroutine terminates with subscription #31025, close XFN-89 (#1687)

This commit is contained in:
Daniel Liu 2025-11-01 14:35:24 +08:00 committed by benjamin202410
parent bbaba9f6af
commit 072f2dc7e2

View file

@ -119,16 +119,23 @@ func NewOracle(backend OracleBackend, params Config, startPrice *big.Int) *Oracl
cache := lru.NewCache[cacheKey, processedFees](2048) cache := lru.NewCache[cacheKey, processedFees](2048)
headEvent := make(chan core.ChainHeadEvent, 1) headEvent := make(chan core.ChainHeadEvent, 1)
backend.SubscribeChainHeadEvent(headEvent) sub := backend.SubscribeChainHeadEvent(headEvent)
if sub != nil { // the gasprice testBackend doesn't support subscribing to head events
go func() { go func() {
var lastHead common.Hash var lastHead common.Hash
for ev := range headEvent { for {
select {
case ev := <-headEvent:
if ev.Block.ParentHash() != lastHead { if ev.Block.ParentHash() != lastHead {
cache.Purge() cache.Purge()
} }
lastHead = ev.Block.Hash() lastHead = ev.Block.Hash()
case <-sub.Err():
return
}
} }
}() }()
}
return &Oracle{ return &Oracle{
backend: backend, backend: backend,