event: improve fix for the goroutine leak in resubscribe

This commit is contained in:
Felix Lange 2020-02-12 12:56:27 +01:00 committed by GitHub
parent da12747636
commit 5968e03e1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -143,9 +143,8 @@ func (s *resubscribeSub) loop() {
}
func (s *resubscribeSub) subscribe() Subscription {
subscribed := make(chan error, 1)
subscribed := make(chan error)
var sub Subscription
retry:
for {
s.lastTry = mclock.Now()
ctx, cancel := context.WithCancel(context.Background())
@ -157,19 +156,19 @@ retry:
select {
case err := <-subscribed:
cancel()
if err != nil {
// Subscribing failed, wait before launching the next try.
if s.backoffWait() {
return nil
}
continue retry
}
if err == nil {
if sub == nil {
panic("event: ResubscribeFunc returned nil subscription and no error")
}
return sub
}
// Subscribing failed, wait before launching the next try.
if s.backoffWait() {
return nil // unsubscribed during wait
}
case <-s.unsub:
cancel()
<-subscribed // avoid leaking the s.fn goroutine.
return nil
}
}