From 5968e03e1e02653f36c15e268713a227a652e3b6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 12 Feb 2020 12:56:27 +0100 Subject: [PATCH] event: improve fix for the goroutine leak in resubscribe --- event/subscription.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/event/subscription.go b/event/subscription.go index 26efbf994b..c80d171f3a 100644 --- a/event/subscription.go +++ b/event/subscription.go @@ -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 + if err == nil { + if sub == nil { + panic("event: ResubscribeFunc returned nil subscription and no error") } - continue retry + return sub } - if sub == nil { - panic("event: ResubscribeFunc returned nil subscription and no error") + // Subscribing failed, wait before launching the next try. + if s.backoffWait() { + return nil // unsubscribed during wait } - return sub case <-s.unsub: cancel() + <-subscribed // avoid leaking the s.fn goroutine. return nil } }