mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
event: improve fix for the goroutine leak in resubscribe
This commit is contained in:
parent
da12747636
commit
5968e03e1e
1 changed files with 9 additions and 10 deletions
|
|
@ -143,9 +143,8 @@ func (s *resubscribeSub) loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *resubscribeSub) subscribe() Subscription {
|
func (s *resubscribeSub) subscribe() Subscription {
|
||||||
subscribed := make(chan error, 1)
|
subscribed := make(chan error)
|
||||||
var sub Subscription
|
var sub Subscription
|
||||||
retry:
|
|
||||||
for {
|
for {
|
||||||
s.lastTry = mclock.Now()
|
s.lastTry = mclock.Now()
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
@ -157,19 +156,19 @@ retry:
|
||||||
select {
|
select {
|
||||||
case err := <-subscribed:
|
case err := <-subscribed:
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
// Subscribing failed, wait before launching the next try.
|
|
||||||
if s.backoffWait() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
continue retry
|
|
||||||
}
|
|
||||||
if sub == nil {
|
if sub == nil {
|
||||||
panic("event: ResubscribeFunc returned nil subscription and no error")
|
panic("event: ResubscribeFunc returned nil subscription and no error")
|
||||||
}
|
}
|
||||||
return sub
|
return sub
|
||||||
|
}
|
||||||
|
// Subscribing failed, wait before launching the next try.
|
||||||
|
if s.backoffWait() {
|
||||||
|
return nil // unsubscribed during wait
|
||||||
|
}
|
||||||
case <-s.unsub:
|
case <-s.unsub:
|
||||||
cancel()
|
cancel()
|
||||||
|
<-subscribed // avoid leaking the s.fn goroutine.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue