[FIXED] Fix Subscription.StatusChanged channel closure on Closed Subscription#2034
Open
nithimani38-prog wants to merge 2 commits intonats-io:mainfrom
Open
[FIXED] Fix Subscription.StatusChanged channel closure on Closed Subscription#2034nithimani38-prog wants to merge 2 commits intonats-io:mainfrom
nithimani38-prog wants to merge 2 commits intonats-io:mainfrom
Conversation
The Subscription.StatusChanged godoc mentions that the returned channel will be closed when the subscription is closed. However, this is the case only if the subscription is closed after the StatusChanged channel is created and registered to listen for changes. To prevent consumers from waiting on signals from closed subscriptions, lets close the channel if the current status of the Subscription is already SubscriptionClosed. We run this check in a defer statement to avoid races against the subscription status changing and allow backward compatibility with usages that expect to receive the current status. Signed-off-by: Nithilan Manimaran Pugazhendhi <nithilan.pugazhendhi@imc.com>
a6edb26 to
c10a05d
Compare
Signed-off-by: Nithilan Manimaran Pugazhendhi <nithilan.pugazhendhi@imc.com>
piotrpio
requested changes
Mar 25, 2026
| ch := make(chan SubStatus, 10) | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| defer func() { |
Collaborator
There was a problem hiding this comment.
With this approach, the listener is already registered (tored in a map) when you're closing the channel. Why not simply return a closed channel if a subscription is closed- not in defer, but before registering it. Since StatusChanged keeps the subscription lock, this should be safe and the subscription will never be closed concurrently.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Subscription.StatusChanged godoc mentions that the returned channel will be closed when the subscription is closed. However, this is the case only if the subscription is closed after the StatusChanged channel is created and registered to listen for changes.
To prevent consumers from waiting on signals from closed subscriptions, lets close the channel if the current status of the Subscription is already SubscriptionClosed. We run this check in a defer statement to avoid races against the subscription status changing and allow backward compatibility with usages that expect to receive the current status.