Skip to content

Commit

Permalink
fix: ethmonitor temporary resource leak (#120)
Browse files Browse the repository at this point in the history
* Fixes: timer resource leak where it can be garbage collected only after it fires

* fix: blockTimer

* restore defaults
  • Loading branch information
marino39 authored Apr 16, 2024
1 parent b69b303 commit 7321126
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ethmonitor/ethmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,15 @@ func (m *Monitor) listenNewHead() <-chan uint64 {
goto reconnect
}

blockTimer := time.NewTimer(3 * m.options.ExpectedBlockInterval)
for {
blockTimer := time.NewTimer(3 * m.options.ExpectedBlockInterval)

select {
case <-m.ctx.Done():
// if we're done, we'll unsubscribe and close the nextBlock channel
sub.Unsubscribe()
close(nextBlock)
blockTimer.Stop()
return

case err := <-sub.Err():
Expand All @@ -350,6 +352,7 @@ func (m *Monitor) listenNewHead() <-chan uint64 {
sub.Unsubscribe()

streamingErrorLastTime = time.Now()
blockTimer.Stop()
goto reconnect

case <-blockTimer.C:
Expand All @@ -361,7 +364,7 @@ func (m *Monitor) listenNewHead() <-chan uint64 {
goto reconnect

case newHead := <-newHeads:
blockTimer.Reset(3 * m.options.ExpectedBlockInterval)
blockTimer.Stop()

latestHeadBlock.Store(newHead.Number.Uint64())
select {
Expand Down Expand Up @@ -395,6 +398,7 @@ func (m *Monitor) listenNewHead() <-chan uint64 {
case <-m.ctx.Done():
// if we're done, we'll close the nextBlock channel
close(nextBlock)
retryStreamingTimer.Stop()
return

case <-time.After(time.Duration(m.pollInterval.Load())):
Expand Down

0 comments on commit 7321126

Please sign in to comment.