fix: memory leak on config reloading#928
Merged
burningalchemist merged 3 commits intomasterfrom Mar 20, 2026
Merged
Conversation
ca59a0d to
42b267c
Compare
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.
resolves #927
This pull request introduces robust resource management for collectors and targets, ensuring that all database connections and prepared statements are properly closed during reloads. This addresses potential memory and goroutine leaks, especially during repeated configuration reloads. It also adds a comprehensive test to detect memory leaks during reload cycles.
Resource management improvements:
Close()method to theCollector,Target, andQueryinterfaces/types, ensuring all prepared statements and database connections are released when no longer needed. (collector.go[1]query.go[2]target.go[3]Close()method for collector and cachingCollector types to release all prepared statements. (collector.gocollector.goR89-R107)Close()method for thetargettype to close all associated collectors and the underlying database connection pool, collecting and reporting any errors. (target.gotarget.goR149-R179)Reload and cleanup logic:
closeTargets()helper, ensuring all old targets' resources are closed before updating to new targets or jobs during a reload. (reload.go[1] [2]closeTargets()helper function to close each target and log (but not propagate) errors, preventing a single failure from blocking the reload process. (reload.goreload.goR97-R112)Testing for leaks:
TestReloadMemoryLeakinreload_test.gothat performs multiple reload cycles and checks for goroutine leaks, ensuring that resource cleanup is effective. (reload_test.goreload_test.goR1-R146)