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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 11 deletions

View File

@ -145,7 +145,6 @@ func (s *resubscribeSub) loop() {
func (s *resubscribeSub) subscribe() Subscription { func (s *resubscribeSub) subscribe() Subscription {
subscribed := make(chan error) 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 sub == nil {
if s.backoffWait() { panic("event: ResubscribeFunc returned nil subscription and no error")
return nil
} }
continue retry return sub
} }
if sub == nil { // Subscribing failed, wait before launching the next try.
panic("event: ResubscribeFunc returned nil subscription and no error") if s.backoffWait() {
return nil // unsubscribed during wait
} }
return sub
case <-s.unsub: case <-s.unsub:
cancel() cancel()
<-subscribed // avoid leaking the s.fn goroutine.
return nil return nil
} }
} }

View File

@ -102,7 +102,7 @@ func TestResubscribe(t *testing.T) {
func TestResubscribeAbort(t *testing.T) { func TestResubscribeAbort(t *testing.T) {
t.Parallel() t.Parallel()
done := make(chan error) done := make(chan error, 1)
sub := Resubscribe(0, func(ctx context.Context) (Subscription, error) { sub := Resubscribe(0, func(ctx context.Context) (Subscription, error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():

View File

@ -287,7 +287,7 @@ func (n *ExecNode) Stop() error {
if err := n.Cmd.Process.Signal(syscall.SIGTERM); err != nil { if err := n.Cmd.Process.Signal(syscall.SIGTERM); err != nil {
return n.Cmd.Process.Kill() return n.Cmd.Process.Kill()
} }
waitErr := make(chan error) waitErr := make(chan error, 1)
go func() { go func() {
waitErr <- n.Cmd.Wait() waitErr <- n.Cmd.Wait()
}() }()