Skip to content

Commit 3faae5d

Browse files
MariusVanDerWijdenfjlholiman
authored
ethstats: avoid creating subscriptions on background goroutine (#22587)
This fixes an issue where the ethstats service could crash if geth was started and then immediately stopped due to an internal error. The cause of the crash was a nil subscription being returned by the backend, because the background goroutine creating them was scheduled after the backend had already shut down. Moving the creation of subscriptions into the Start method, which runs synchronously during startup of the node, means the returned subscriptions can never be 'nil'. Co-authored-by: Felix Lange <fjl@twurst.com> Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 44fe466 commit 3faae5d

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

‎ethstats/ethstats.go‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ type Service struct {
9595
pongCh chan struct{} // Pong notifications are fed into this channel
9696
histCh chan []uint64 // History request block numbers are fed into this channel
9797

98+
headSub event.Subscription
99+
txSub event.Subscription
98100
}
99101

100102
// connWrapper is a wrapper to prevent concurrent-write or concurrent-read on the
@@ -167,30 +169,28 @@ func New(node *node.Node, backend backend, engine consensus.Engine, url string)
167169

168170
// Start implements node.Lifecycle, starting up the monitoring and reporting daemon.
169171
func (s *Service) Start() error {
170-
go s.loop()
172+
// Subscribe to chain events to execute updates on
173+
chainHeadCh := make(chan core.ChainHeadEvent, chainHeadChanSize)
174+
s.headSub = s.backend.SubscribeChainHeadEvent(chainHeadCh)
175+
txEventCh := make(chan core.NewTxsEvent, txChanSize)
176+
s.txSub = s.backend.SubscribeNewTxsEvent(txEventCh)
177+
go s.loop(chainHeadCh, txEventCh)
171178

172179
log.Info("Stats daemon started")
173180
return nil
174181
}
175182

176183
// Stop implements node.Lifecycle, terminating the monitoring and reporting daemon.
177184
func (s *Service) Stop() error {
185+
s.headSub.Unsubscribe()
186+
s.txSub.Unsubscribe()
178187
log.Info("Stats daemon stopped")
179188
return nil
180189
}
181190

182191
// loop keeps trying to connect to the netstats server, reporting chain events
183192
// until termination.
184-
func (s *Service) loop() {
185-
// Subscribe to chain events to execute updates on
186-
chainHeadCh := make(chan core.ChainHeadEvent, chainHeadChanSize)
187-
headSub := s.backend.SubscribeChainHeadEvent(chainHeadCh)
188-
defer headSub.Unsubscribe()
189-
190-
txEventCh := make(chan core.NewTxsEvent, txChanSize)
191-
txSub := s.backend.SubscribeNewTxsEvent(txEventCh)
192-
defer txSub.Unsubscribe()
193-
193+
func (s *Service) loop(chainHeadCh chan core.ChainHeadEvent, txEventCh chan core.NewTxsEvent) {
194194
// Start a goroutine that exhausts the subscriptions to avoid events piling up
195195
var (
196196
quitCh = make(chan struct{})
@@ -223,9 +223,9 @@ func (s *Service) loop() {
223223
}
224224

225225
// node stopped
226-
case <-txSub.Err():
226+
case <-s.txSub.Err():
227227
break HandleLoop
228-
case <-headSub.Err():
228+
case <-s.headSub.Err():
229229
break HandleLoop
230230
}
231231
}

0 commit comments

Comments
 (0)