fix(queue): prevent Broadcast from blocking on closed/empty channels - #5411
Open
Hlgxz wants to merge 3 commits into
Open
fix(queue): prevent Broadcast from blocking on closed/empty channels#5411Hlgxz wants to merge 3 commits into
Hlgxz wants to merge 3 commits into
Conversation
Use non-blocking select statement in Broadcast to avoid goroutine leaks when consumers have exited but their event channels are still in the list. The issue occurs when: - Consumer goroutines exit (after q.channel closes) - eventChannels list is not cleaned up - Broadcast tries to send to channels with no receivers - This causes permanent blocking and goroutine leaks Fix uses select with default case to skip channels that cannot receive, preventing blocking and goroutine leaks. Co-authored-by: Cursor <cursoragent@cursor.com>
kevwan
force-pushed
the
fix/queue-broadcast-blocking
branch
from
February 15, 2026 08:24
14fab24 to
0a0232c
Compare
Author
|
@kevwan Could you please take a look at this fix? All existing tests are passing, and we’d appreciate getting this merged soon. The change uses a non‑blocking send to avoid goroutine leaks when consumers have already exited. Thanks! |
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.
🐛 Bug Fix: Prevent Queue.Broadcast from Blocking on Closed/Empty Channels
Problem Description
The
Queue.Broadcastmethod has a potential goroutine leak issue when consumers have exited but their event channels are still in theeventChannelslist. This can cause the Broadcast goroutine to block permanently when trying to send messages to channels with no receivers.Root Cause Analysis
Channel Lifecycle Issue:
startConsumerscreates consumers, it creates unbufferedeventChanchannels and adds them toeventChannelslistq.channelcloses), theconsumefunction returns, but theeventChanis never removed fromeventChannelsBlocking Behavior:
Broadcastsends messages to all channels ineventChannelsusing blocking send:channel <- messageeventChaneventChanis an unbuffered channel, the send operation blocks foreverReproduction Steps
Solution
Use a non-blocking
selectstatement with adefaultcase to skip channels that cannot receive messages:This approach:
Testing
TestQueue_BroadcastRelated Issues
This fix addresses a potential resource leak that could occur in long-running applications where Queue instances are reused or where Broadcast is called after consumers have exited.