Skip to content

Commit 146bab9

Browse files
Copilotanacrolix
andcommitted
Backport TestResubscribeWithCompletedSubscription test and fix deadlock
Co-authored-by: anacrolix <988750+anacrolix@users.noreply.github.com>
1 parent b28d663 commit 146bab9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

p2p/event/subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func ResubscribeErr(backoffMax time.Duration, fn ResubscribeErrFunc) Subscriptio
123123
backoffMax: backoffMax,
124124
fn: fn,
125125
err: make(chan error),
126-
unsub: make(chan struct{}),
126+
unsub: make(chan struct{}, 1),
127127
}
128128
go s.loop()
129129
return s

p2p/event/subscription_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,27 @@ func TestResubscribeWithErrorHandler(t *testing.T) {
156156
t.Fatalf("unexpected subscription errors %v, want %v", subErrs, expectedSubErrs)
157157
}
158158
}
159+
160+
func TestResubscribeWithCompletedSubscription(t *testing.T) {
161+
t.Parallel()
162+
163+
quitProducerAck := make(chan struct{})
164+
quitProducer := make(chan struct{})
165+
166+
sub := ResubscribeErr(100*time.Millisecond, func(ctx context.Context, lastErr error) (Subscription, error) {
167+
return NewSubscription(func(unsubscribed <-chan struct{}) error {
168+
select {
169+
case <-quitProducer:
170+
quitProducerAck <- struct{}{}
171+
return nil
172+
case <-unsubscribed:
173+
return nil
174+
}
175+
}), nil
176+
})
177+
178+
// Ensure producer has started and exited before Unsubscribe
179+
close(quitProducer)
180+
<-quitProducerAck
181+
sub.Unsubscribe()
182+
}

0 commit comments

Comments
 (0)