Repository navigation
fix: resolve memory leaks in several places [backport #1060] - #1062
Conversation
Three resource leak fixes identified from memory profiling: 1. pkg/zstd/zstd.go: Add runtime.SetFinalizer on pooled zstd.Decoder instances. Each decoder spawns background goroutines that hold a back-reference to it, preventing GC after sync.Pool clears the entry via the victim cache. This causes linear memory growth (~10 GB/hr under load). The finalizer calls Close() when the decoder becomes unreachable, stopping its goroutines. 2. pkg/cache/upstream/cache.go: Close non-200 HTTP response bodies after draining them. Without Close(), the HTTP transport cannot return connections to the pool, causing exhaustion under error conditions. 3. pkg/cache/cache.go: Close the storage reader when the database transaction fails in getNarFromStore. The reader (which may wrap a zstd decoder with background goroutines) was being silently dropped, leaking file handles. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> (cherry picked from commit 3bb7739)
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses several critical resource and memory leaks identified through profiling. The changes focus on ensuring proper closure of Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively resolves several memory leaks. Key fixes include adding a finalizer to pooled zstd.Decoder instances to prevent goroutine leaks, ensuring HTTP response bodies are closed for non-200 status codes to avoid connection exhaustion, and closing storage readers on transaction failures to prevent file handle leaks. The introduction of a generic helper.MultiReadCloser is a notable improvement, refactoring the code to robustly handle the closing of chained I/O resources in multiple locations. The changes are well-targeted, and the new tests provide good coverage for the fixes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release-0.9 #1062 +/- ##
===========================================
Coverage 5.04% 5.04%
===========================================
Files 6 6
Lines 436 436
===========================================
Hits 22 22
Misses 410 410
Partials 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bot-based backport to
release-0.9, triggered by a label in #1060.Three resource leak fixes identified from memory profiling:
pkg/zstd/zstd.go: Add runtime.SetFinalizer on pooled zstd.Decoder
instances. Each decoder spawns background goroutines that hold a
back-reference to it, preventing GC after sync.Pool clears the entry
via the victim cache. This causes linear memory growth (~10 GB/hr
under load). The finalizer calls Close() when the decoder becomes
unreachable, stopping its goroutines.
pkg/cache/upstream/cache.go: Close non-200 HTTP response bodies after
draining them. Without Close(), the HTTP transport cannot return
connections to the pool, causing exhaustion under error conditions.
pkg/cache/cache.go: Close the storage reader when the database
transaction fails in getNarFromStore. The reader (which may wrap a
zstd decoder with background goroutines) was being silently dropped,
leaking file handles.