Repository navigation
fix: destroy leaked RocksDB native handles in CompactFiles - #206
Merged
Merged
Conversation
CompactFiles creates a grocksdb.WriteBatch (and its enclosing iterator's ReadOptions) but never destroys them, and commit() allocates a fresh WriteOptions inline without destroying it. fileCompactionLoop calls CompactFiles in a tight loop, so each of these native rocksdb_* handles leaks once per run on every path — normal, cancellation, and error — growing process RSS unbounded under a sustained compaction backlog. Defer Destroy() on all three (matching the pattern recompactor.go already uses). ro is deferred before the iterator so it is released after the iterator that references it is closed; Write() does not consume the batch, so a top-level defer on wb is valid through commit. make test-storage passes (incl. storage/compaction). Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01YCoN4Him5ZY4hZovVp37ra Signed-off-by: Ovais Tariq <[email protected]>
efirs
approved these changes
Aug 14, 2026
Greptile SummaryThis PR fixes native RocksDB handle leaks in file compaction without changing compaction behavior.
Confidence Score: 5/5The PR appears safe to merge with no actionable correctness, security, or resource-lifetime issues identified. The new defers cover all return paths, preserve the required iterator-before-read-options cleanup order, and release write resources only after synchronous RocksDB operations finish. Important Files Changed
Reviews (1): Last reviewed commit: "fix: destroy leaked RocksDB native handl..." | Re-trigger Greptile |
|
🎉 This PR is included in version 1.10.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Problem
Compactor.CompactFiles(storage/compaction/compactor.go) allocates three RocksDB native handles that are never released:wb := grocksdb.NewWriteBatch()(line 258) — noDestroy()on any path.ro := metadata.CreateReadOptions(...)(line 254) — the iterator's read options, noDestroy().commit()allocatesgrocksdb.NewDefaultWriteOptions()inline (line 669) — noDestroy().fileCompactionLoopinvokesCompactFilesin a tight loop (it only sleeps ~100 ms after a run reports zero processed, and otherwise starts the next run immediately). So each of theserocksdb_*_createhandles leaks once per run — across the normal, cancellation, and error returns alike — and process RSS grows unbounded under a sustained compaction backlog. This is native (off-Go-heap) memory, directly relevant to the RSS work in #176.The sibling file
storage/compaction/recompactor.goalready does this correctly (defer wb.Destroy()at :179,defer wo.Destroy()at :248/:422), which is the pattern applied here.Fix
defer Destroy()on all three.rois deferred before the iterator'sdefer it.Close()so that at return the iterator (which referencesro) is closed first androis destroyed after (defers run LIFO).Write()does not consume the batch, so a top-leveldefer wb.Destroy()remains valid throughcommit().Test
make test-storagepasses, includingstorage/compaction(exit 0). No behavior change — only handle lifetimes.Provenance
Surfaced by a Perfloop frontier case (
case_wwm7tex9mn, patternallocation_hot_loop) and confirmed by direct source inspection before fixing.🤖 Generated with Claude Code
Note
Low Risk
Lifecycle-only changes to RocksDB handle cleanup with no altered compaction semantics; aligns with an established pattern elsewhere in the same package.
Overview
Fixes unbounded native memory growth when file compaction workers run continuously by releasing RocksDB C++ handles that were created on every
CompactFilesinvocation.CompactFilesnowdefersDestroy()on the read options, write batch, and (incommit) write options, with read options torn down after the iterator closes (LIFO defers). Inline comments document the tightfileCompactionLoopcall pattern and thatWrite()does not consume the batch. This mirrors the existing pattern inrecompactor.go. No compaction logic or on-disk behavior changes—only handle lifetimes.Reviewed by Cursor Bugbot for commit 52194ab. Bugbot is set up for automated code reviews on this repo. Configure here.