Skip to content

fix: memory leak on config reloading#928

Merged
burningalchemist merged 3 commits intomasterfrom
fix/memleak
Mar 20, 2026
Merged

fix: memory leak on config reloading#928
burningalchemist merged 3 commits intomasterfrom
fix/memleak

Conversation

@burningalchemist
Copy link
Owner

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:

  • Added a Close() method to the Collector, Target, and Query interfaces/types, ensuring all prepared statements and database connections are released when no longer needed. (collector.go [1] query.go [2] target.go [3]
  • Implemented the Close() method for collector and cachingCollector types to release all prepared statements. (collector.go collector.goR89-R107)
  • Implemented the Close() method for the target type to close all associated collectors and the underlying database connection pool, collecting and reporting any errors. (target.go target.goR149-R179)

Reload and cleanup logic:

  • Updated reload logic to call a new closeTargets() helper, ensuring all old targets' resources are closed before updating to new targets or jobs during a reload. (reload.go [1] [2]
  • Added the closeTargets() helper function to close each target and log (but not propagate) errors, preventing a single failure from blocking the reload process. (reload.go reload.goR97-R112)

Testing for leaks:

  • Added a new test TestReloadMemoryLeak in reload_test.go that performs multiple reload cycles and checks for goroutine leaks, ensuring that resource cleanup is effective. (reload_test.go reload_test.goR1-R146)

@burningalchemist burningalchemist changed the title fix: memory leak on reload fix: memory leak on config reloading Mar 20, 2026
@burningalchemist burningalchemist merged commit 9c1f94d into master Mar 20, 2026
4 checks passed
@burningalchemist burningalchemist deleted the fix/memleak branch March 20, 2026 17:29
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.

Memory consumption keeps increasing over time

1 participant