Skip to content

Commit

Permalink
event, p2p/simulations/adapters: fix rare goroutine leaks (ethereum#2…
Browse files Browse the repository at this point in the history
…0657)

Co-authored-by: Felix Lange <[email protected]>
  • Loading branch information
Boqin Qin and fjl authored Feb 12, 2020
1 parent 46c4b69 commit a9614c3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
17 changes: 8 additions & 9 deletions event/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion event/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestResubscribe(t *testing.T) {
func TestResubscribeAbort(t *testing.T) {
t.Parallel()

done := make(chan error)
done := make(chan error, 1)
sub := Resubscribe(0, func(ctx context.Context) (Subscription, error) {
select {
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion p2p/simulations/adapters/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (n *ExecNode) Stop() error {
if err := n.Cmd.Process.Signal(syscall.SIGTERM); err != nil {
return n.Cmd.Process.Kill()
}
waitErr := make(chan error)
waitErr := make(chan error, 1)
go func() {
waitErr <- n.Cmd.Wait()
}()
Expand Down

0 comments on commit a9614c3

Please sign in to comment.