-
Notifications
You must be signed in to change notification settings - Fork 20.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
event: fix Resubscribe deadlock when unsubscribing after inner sub ends #28359
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,3 +154,23 @@ func TestResubscribeWithErrorHandler(t *testing.T) { | |
t.Fatalf("unexpected subscription errors %v, want %v", subErrs, expectedSubErrs) | ||
} | ||
} | ||
|
||
func TestResubscribeWithCompletedSubscription(t *testing.T) { | ||
t.Parallel() | ||
|
||
innerSubDone := make(chan struct{}, 1) | ||
sub := ResubscribeErr(100*time.Millisecond, func(ctx context.Context, lastErr error) (Subscription, error) { | ||
return NewSubscription(func(unsubscribed <-chan struct{}) error { | ||
select { | ||
case <-time.After(2 * time.Second): | ||
innerSubDone <- struct{}{} | ||
return nil | ||
case <-unsubscribed: | ||
return nil | ||
} | ||
}), nil | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not too familiar with how the semantics of subscriptions work in any depth, but I've been trying to figure out if this is 'correct' or not. The docs for NewSubscription says
In other words, If the select is changed into select {
case <-time.After(2 * time.Second):
innerSubDone <- struct{}{}
return errors.New("time to go")
case <-unsubscribed:
return nil
} Then the deadlock disappears. So my thinking goes: this testcase is based on a flawed producer. But also, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Clarifying the behavior of producers would be a better fix in that case. Will do that instead. Perhaps it'll be good to add defensive checks against There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, though: I'm no authority here. @fjl would know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that this documentation already clarifies the intended behavior based on |
||
|
||
<-innerSubDone | ||
sub.Unsubscribe() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's weird to have a timeout here. You can achieve the necessary synchronization event with another channel, like so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good suggestion. Made the change.