Fix goroutine leak in queue package#367
Open
lxcxjxhx wants to merge 3 commits into
Open
Conversation
- Fix double timeout padding in Handle() method that inflated user-requested timeouts by 5 seconds - Fix incorrect log type in WriteFile() method (stdin -> stdout) for proper log stream classification - Fix incomplete file read in ReadFile() method by using io.ReadFull instead of single Read() call These bugs affected command execution timeout handling, log type correctness, and file read completeness. All fixes are minimal, targeted, and maintain backward compatibility while improving correctness.
- Remove defer close(q.queue) from reader() to prevent double close - Add explicit close(q.queue) in Stop() after cancel() to ensure workers exit - This fixes goroutine leak when Stop() is called while workers are still processing The issue: when Stop() calls cancel(), the reader() might not exit immediately, and workers waiting on q.queue channel would block forever, causing goroutine leak.
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.
Fix Goroutine Leak in Queue Package
Problem
The
queuepackage has a goroutine leak issue in theStop()method. WhenStop()is called while workers are still processing messages, the following sequence occurs:Stop()callsq.cancel()to signal shutdownreader()receives the cancellation signal and exitsq.queuechannel may not exit immediatelyRoot Cause
The issue stems from the interaction between
reader()andStop():reader()hasdefer close(q.queue)which closes the channel when it exitsStop()callsq.cancel()but doesn't explicitly close the queue channelreader()hasn't exited yet whenStop()checksq.ctx.Err(), workers can be left waiting on an open channelSolution
defer close(q.queue)fromreader(): This prevents potential double-close panicsclose(q.queue)inStop(): After callingq.cancel(), immediately close the queue channel to ensure all workers can exit their receive loopsChanges
backend/pkg/queue/queue.go: ModifiedStop()andreader()methodsclose(q.queue)afterq.cancel()inStop()defer close(q.queue)fromreader()Impact
Stop()is calledTesting
The fix ensures that:
Stop()is called