Skip to content

Conversation

@travisshivers
Copy link

This cherry-picks an upstream PR r3labs#178

readLoop was leaking goroutines when subscribers were unsubscribed, because it was blocked trying to write a nil to erChan in response to an upstream io.EOF.

This commit passes in a context to startReadLoop, ensuring that when that context is closed, the read loop terminates correctly.

For SubscribeWithContext we just pass in the parent context, as there is no way to unsubscribe the handler.

For SubscribeChanWithContext, we pass in a derived cancelable context, which we cancel explicitly when the channel is unsubscribed.

readLoop was leaking goroutines when subscribers were unsubscribed,
because it was blocked trying to write a nil to erChan in response to an
upstream io.EOF.

This commit passes in a context to startReadLoop, ensuring that when
that context is closed, the read loop terminates correctly.

For SubscribeWithContext we just pass in the parent context, as there is
no way to unsubscribe the handler.

For SubscribeChanWithContext, we pass in a derived cancelable context,
which we cancel explicitly when the channel is unsubscribed.
@travisshivers travisshivers merged commit 628ccfa into main Apr 28, 2025
@travisshivers travisshivers deleted the fix-leak branch April 28, 2025 14:16
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.

4 participants