fix: [sc-89434] Close msgQueue per reconnect cycle to prevent goroutine leak#60
Merged
Merged
Conversation
…ne leak Move msgQueue creation and worker pool into each iteration of the MQTT reconnection loop. Add drainWorkers() (close + WaitGroup.Wait()) at every exit point — connection lost, subscribe failure, connect failure, stop signal, and options error — so worker goroutines from each cycle drain and exit before the next cycle begins. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
msgQueuewas created once before the reconnection loop and never closed, so workers had no signal to exit between cycles.msgQueuecreation and the worker pool are now moved inside the reconnection loop so each connection attempt gets a fresh queue and a freshsync.WaitGroup.drainWorkers()closure (close(msgQueue)+wg.Wait()) is called at every exit point before the loop resets: connection lost, subscribe failure, connect failure, stop signal, and MQTT options error. Workers drain any buffered messages and exit cleanly before the next cycle begins.Changes
cmd/agent_smith/service.go: removed pre-loop worker/queue setup; added per-iteration worker pool withsync.WaitGroupanddrainWorkers()calls at all five exit/continue paths; added"sync"import.cmd/agent_smith/message_queue_test.go: addedTestWorkerPool_NoLeakAcrossReconnectCycles— runs 5 simulated connection cycles, each creating workers, enqueuing a message, closing the queue, and waiting; asserts goroutine count does not grow beyond baseline + 3.Test plan
go test ./cmd/agent_smith/...— all tests pass includingTestWorkerPool_NoLeakAcrossReconnectCyclesgo tool pprofor/debug/pprof/goroutine