event, p2p/simulations/adapters: fix rare goroutine leaks (#20657)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Boqin Qin
2020-02-12 22:19:47 +08:00
committed by GitHub
parent 46c4b699c8
commit a9614c3c91
3 changed files with 10 additions and 11 deletions

View File

@ -145,7 +145,6 @@ func (s *resubscribeSub) loop() {
func (s *resubscribeSub) subscribe() Subscription {
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
}
}