Repository navigation
fix: resolve memory leaks in several places - #1060
Conversation
…ions, and storage reader 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.
|
This change is part of the following stack: Change managed by git-spice. |
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 implements crucial fixes to prevent several resource leaks identified through memory profiling. The changes ensure proper cleanup of zstd decoder instances, efficient management of HTTP connections by closing response bodies, and correct handling of storage readers during database transaction failures. These improvements are vital for maintaining application stability and preventing gradual memory and file descriptor exhaustion under load. 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 aims to resolve three distinct resource leaks by adding a runtime.SetFinalizer to pooled zstd.Decoder instances, properly closing non-200 HTTP response bodies in the upstream cache logic, and ensuring a storage reader is closed in a previously unhandled error path. However, a critical vulnerability remains: the fix in pkg/cache/cache.go is incomplete as it fails to close the underlying reader when transparent decompression is used, still resulting in a resource leak. The other fixes in pkg/zstd/zstd.go and pkg/cache/upstream/cache.go appear correct and follow best practices.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1060 +/- ##
=====================================
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:
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request effectively addresses three distinct and important resource leaks, significantly improving application stability. The fixes for zstd.Decoder goroutine leaks, unclosed HTTP response bodies, and unclosed storage readers are all well-implemented. The introduction of a generic MultiReadCloser is a good refactoring that improves code clarity and prevents future leaks. Overall, these are excellent changes.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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)
|
Successfully created backport PR for |
Bot-based backport to `release-0.9`, triggered by a label in #1060. 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>
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.