Skip to content

Commit 6e9aa18

Browse files
committed
eth/gasprice: ensure cache purging goroutine terminates with subscription ethereum#31025
1 parent 22edaac commit 6e9aa18

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

eth/gasprice/gasprice.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,23 @@ func NewOracle(backend OracleBackend, params Config, startPrice *big.Int) *Oracl
119119

120120
cache := lru.NewCache[cacheKey, processedFees](2048)
121121
headEvent := make(chan core.ChainHeadEvent, 1)
122-
backend.SubscribeChainHeadEvent(headEvent)
123-
go func() {
124-
var lastHead common.Hash
125-
for ev := range headEvent {
126-
if ev.Block.ParentHash() != lastHead {
127-
cache.Purge()
122+
sub := backend.SubscribeChainHeadEvent(headEvent)
123+
if sub != nil { // the gasprice testBackend doesn't support subscribing to head events
124+
go func() {
125+
var lastHead common.Hash
126+
for {
127+
select {
128+
case ev := <-headEvent:
129+
if ev.Block.ParentHash() != lastHead {
130+
cache.Purge()
131+
}
132+
lastHead = ev.Block.Hash()
133+
case <-sub.Err():
134+
return
135+
}
128136
}
129-
lastHead = ev.Block.Hash()
130-
}
131-
}()
137+
}()
138+
}
132139

133140
return &Oracle{
134141
backend: backend,

0 commit comments

Comments
 (0)