Skip to content

fix: destroy leaked RocksDB native handles in CompactFiles - #206

Merged
ovaistariq merged 1 commit into
mainfrom
fix/compactfiles-native-handle-leaks
Aug 14, 2026
Merged

ovaistariq merged 1 commit into
mainfrom
fix/compactfiles-native-handle-leaks

Conversation

@ovaistariq

@ovaistariq ovaistariq commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Compactor.CompactFiles (storage/compaction/compactor.go) allocates three RocksDB native handles that are never released:

  • wb := grocksdb.NewWriteBatch() (line 258) — no Destroy() on any path.
  • ro := metadata.CreateReadOptions(...) (line 254) — the iterator's read options, no Destroy().
  • commit() allocates grocksdb.NewDefaultWriteOptions() inline (line 669) — no Destroy().

fileCompactionLoop invokes CompactFiles in a tight loop (it only sleeps ~100 ms after a run reports zero processed, and otherwise starts the next run immediately). So each of these rocksdb_*_create handles 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.go already 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. ro is deferred before the iterator's defer it.Close() so that at return the iterator (which references ro) is closed first and ro is destroyed after (defers run LIFO). Write() does not consume the batch, so a top-level defer wb.Destroy() remains valid through commit().

Test

make test-storage passes, including storage/compaction (exit 0). No behavior change — only handle lifetimes.

Provenance

Surfaced by a Perfloop frontier case (case_wwm7tex9mn, pattern allocation_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 CompactFiles invocation.

CompactFiles now defers Destroy() on the read options, write batch, and (in commit) write options, with read options torn down after the iterator closes (LIFO defers). Inline comments document the tight fileCompactionLoop call pattern and that Write() does not consume the batch. This mirrors the existing pattern in recompactor.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.

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]>
@greptile-apps

greptile-apps Bot commented Aug 14, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes native RocksDB handle leaks in file compaction without changing compaction behavior.

  • Destroys iterator read options after the iterator closes.
  • Destroys each CompactFiles write batch on every return path.
  • Destroys write options after each synchronous metadata write.

Confidence Score: 5/5

The 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

Filename Overview
storage/compaction/compactor.go Adds correctly ordered cleanup for three RocksDB native handles, matching established sibling compaction patterns with no identified regressions.

Reviews (1): Last reviewed commit: "fix: destroy leaked RocksDB native handl..." | Re-trigger Greptile

@ovaistariq
ovaistariq merged commit bf67fc7 into main Aug 14, 2026
13 of 14 checks passed
@ovaistariq
ovaistariq deleted the fix/compactfiles-native-handle-leaks branch August 14, 2026 18:36
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.10.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants