Skip to content

Commit a9614c3

Browse files
Boqin Qinfjl
andauthored
event, p2p/simulations/adapters: fix rare goroutine leaks (#20657)
Co-authored-by: Felix Lange <[email protected]>
1 parent 46c4b69 commit a9614c3

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

event/subscription.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ func (s *resubscribeSub) loop() {
145145
func (s *resubscribeSub) subscribe() Subscription {
146146
subscribed := make(chan error)
147147
var sub Subscription
148-
retry:
149148
for {
150149
s.lastTry = mclock.Now()
151150
ctx, cancel := context.WithCancel(context.Background())
@@ -157,19 +156,19 @@ retry:
157156
select {
158157
case err := <-subscribed:
159158
cancel()
160-
if err != nil {
161-
// Subscribing failed, wait before launching the next try.
162-
if s.backoffWait() {
163-
return nil
159+
if err == nil {
160+
if sub == nil {
161+
panic("event: ResubscribeFunc returned nil subscription and no error")
164162
}
165-
continue retry
163+
return sub
166164
}
167-
if sub == nil {
168-
panic("event: ResubscribeFunc returned nil subscription and no error")
165+
// Subscribing failed, wait before launching the next try.
166+
if s.backoffWait() {
167+
return nil // unsubscribed during wait
169168
}
170-
return sub
171169
case <-s.unsub:
172170
cancel()
171+
<-subscribed // avoid leaking the s.fn goroutine.
173172
return nil
174173
}
175174
}

event/subscription_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestResubscribe(t *testing.T) {
102102
func TestResubscribeAbort(t *testing.T) {
103103
t.Parallel()
104104

105-
done := make(chan error)
105+
done := make(chan error, 1)
106106
sub := Resubscribe(0, func(ctx context.Context) (Subscription, error) {
107107
select {
108108
case <-ctx.Done():

p2p/simulations/adapters/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (n *ExecNode) Stop() error {
287287
if err := n.Cmd.Process.Signal(syscall.SIGTERM); err != nil {
288288
return n.Cmd.Process.Kill()
289289
}
290-
waitErr := make(chan error)
290+
waitErr := make(chan error, 1)
291291
go func() {
292292
waitErr <- n.Cmd.Wait()
293293
}()

0 commit comments

Comments
 (0)