Skip to content

fix(queue): prevent Broadcast from blocking on closed/empty channels - #5411

Open
Hlgxz wants to merge 3 commits into
zeromicro:masterfrom
Hlgxz:fix/queue-broadcast-blocking
Open

fix(queue): prevent Broadcast from blocking on closed/empty channels#5411
Hlgxz wants to merge 3 commits into
zeromicro:masterfrom
Hlgxz:fix/queue-broadcast-blocking

Conversation

@Hlgxz

@Hlgxz Hlgxz commented Feb 9, 2026

Copy link
Copy Markdown

🐛 Bug Fix: Prevent Queue.Broadcast from Blocking on Closed/Empty Channels

Problem Description

The Queue.Broadcast method has a potential goroutine leak issue when consumers have exited but their event channels are still in the eventChannels list. This can cause the Broadcast goroutine to block permanently when trying to send messages to channels with no receivers.

Root Cause Analysis

  1. Channel Lifecycle Issue:

    • When startConsumers creates consumers, it creates unbuffered eventChan channels and adds them to eventChannels list
    • When consumers exit (after q.channel closes), the consume function returns, but the eventChan is never removed from eventChannels
    • The channels are never closed, leaving stale references in the list
  2. Blocking Behavior:

    • Broadcast sends messages to all channels in eventChannels using blocking send: channel <- message
    • If a consumer has exited, there's no receiver for its eventChan
    • Since eventChan is an unbuffered channel, the send operation blocks forever
    • This causes goroutine leaks as the Broadcast goroutine never completes

Reproduction Steps

// Scenario that can trigger the issue:
q := NewQueue(producerFactory, consumerFactory)
q.Start()  // Consumers start and eventChannels are populated

// ... later, producers finish and q.channel closes
// Consumers exit, but eventChannels list is not cleaned up

// If Broadcast is called after consumers exit:
q.Broadcast("message")  // Goroutine leaks - blocks forever on stale channels

Solution

Use a non-blocking select statement with a default case to skip channels that cannot receive messages:

for _, channel := range q.eventChannels {
    select {
    case channel <- message:
        // Successfully sent
    default:
        // Channel is full or has no receiver, skip to avoid blocking
        // This can happen if the consumer has exited but the channel
        // is still in the eventChannels list
    }
}

This approach:

  • Prevents goroutine leaks by avoiding permanent blocking

Testing

  • All existing tests pass, including TestQueue_Broadcast
  • The fix handles both normal operation (consumers active) and edge cases (consumers exited)

Related 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.

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
kevwan force-pushed the fix/queue-broadcast-blocking branch from 14fab24 to 0a0232c Compare February 15, 2026 08:24
@Hlgxz

Hlgxz commented Jun 23, 2026

Copy link
Copy Markdown
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!

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.

1 participant