Skip to content

[FIXED] Fix Subscription.StatusChanged channel closure on Closed Subscription#2034

Open
nithimani38-prog wants to merge 2 commits intonats-io:mainfrom
nithimani38-prog:close-status-changed-channel
Open

[FIXED] Fix Subscription.StatusChanged channel closure on Closed Subscription#2034
nithimani38-prog wants to merge 2 commits intonats-io:mainfrom
nithimani38-prog:close-status-changed-channel

Conversation

@nithimani38-prog
Copy link
Copy Markdown

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.

@nithimani38-prog nithimani38-prog changed the title [WIP] Fix Subscription.StatusChanged channel closure on Closed Subscription [FIXED] Fix Subscription.StatusChanged channel closure on Closed Subscription Mar 3, 2026
@nithimani38-prog nithimani38-prog marked this pull request as ready for review March 3, 2026 23:02
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 85.535% (+0.1%) from 85.426%
when pulling a6edb26 on nithimani38-prog:close-status-changed-channel
into 9941f25 on nats-io:main.

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>
@nithimani38-prog nithimani38-prog force-pushed the close-status-changed-channel branch from a6edb26 to c10a05d Compare March 5, 2026 20:15
Signed-off-by: Nithilan Manimaran Pugazhendhi <nithilan.pugazhendhi@imc.com>
ch := make(chan SubStatus, 10)
s.mu.Lock()
defer s.mu.Unlock()
defer func() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants