From 0459203710df20f2da989cc6df2dbbb3765baeb7 Mon Sep 17 00:00:00 2001 From: kamuikatsurgi Date: Mon, 27 Apr 2026 17:33:17 +0530 Subject: [PATCH] eth: cancel milestone subscriber on shutdown --- eth/backend.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index 1b51bfde4d..bd2b3cb23c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -754,8 +754,9 @@ func (s *Ethereum) startCheckpointWhitelistService() { s.retryHeimdallHandler(s.fetchAndHandleWhitelistCheckpoint, tickerDuration, whitelistTimeout) } -// startMilestoneWhitelistService starts the goroutine to fetch milestiones and update the -// milestone whitelist map. +// startMilestoneWhitelistService starts the goroutine that updates the milestone whitelist map. +// It subscribes to milestone events from heimdall via websocket when available, and falls back +// to polling otherwise. func (s *Ethereum) startMilestoneWhitelistService() { ethHandler, bor, _ := s.getHandler() @@ -763,10 +764,17 @@ func (s *Ethereum) startMilestoneWhitelistService() { tickerDuration = 2 * time.Second ) - // If heimdall ws is available use WS subscription to new milestone events instead of polling + // If heimdall WS is available, use WS subscription for new milestone events instead of polling if bor != nil && bor.HeimdallWSClient != nil { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + go func() { + <-s.closeCh + cancel() + }() + for { - if err := s.subscribeAndHandleMilestone(context.Background(), ethHandler, bor); err != nil { + if err := s.subscribeAndHandleMilestone(ctx, ethHandler, bor); err != nil { log.Error("Error subscribing to milestone events", "err", err) } @@ -775,7 +783,7 @@ func (s *Ethereum) startMilestoneWhitelistService() { case <-s.closeCh: return case <-time.After(tickerDuration): - // Continue to retry subscribing to milestone event + // Continue to retry subscribing to milestone events } } }