[server][dvc] Fix use-after-free in RMD block cache metrics callbacks - #2742
Merged
sushantmane merged 2 commits intoApr 15, 2026
Merged
Conversation
The RMD block cache async gauge callbacks captured `rmdCache` directly via method references (`rmdCache::getUsage`, `rmdCache::getPinnedUsage`) with no lifecycle guard. When `RocksDBStorageEngineFactory.close()` frees the native Cache object, the OTel/Tehuti callbacks still fire on the next 60-second collection cycle, dereferencing the stale JNI handle and causing a SIGSEGV. This caused 21+ JVM crashes across EI and prod over 6 days. 82% of crashes were on the `collectionWindowExecutor-1` (OTel) or `Async_Gauge_Executor` (Tehuti) metrics threads. The dominant crash signature was a classic C++ vtable use-after-free: the freed Cache memory was reused by the Java heap, so virtual method dispatch jumped to garbage addresses containing Java class metadata. The existing partition-level metrics (block-cache-capacity, etc.) are safe because they go through `getRocksDBStatValue()` which is guarded by `synchronized(hostedRocksDBPartitions)` and `readCloseRWLock`. The RMD cache callbacks bypassed all of these guards. Fix: Check `rmdCache.isOwningHandle()` before each JNI call. After `RocksObject.close()`, `isOwningHandle()` returns false, preventing the stale native pointer dereference.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes JVM crashes (SIGSEGV) caused by use-after-free when sampling RocksDB RMD block cache metrics via async gauge callbacks, by guarding JNI calls with an ownership check.
Changes:
- Wrap RMD block cache
usageandpinned_usageasync gauge suppliers with anisOwningHandle()check and return0when the handle is non-owning.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replace isOwningHandle() check with a stronger pattern: store the Cache in a volatile field, and have callbacks read it into a local variable before null-checking. The local pins the reference on the stack so it cannot become null between check and JNI call — no locks needed. Add closeRMDBlockCache() called from RocksDBStorageEngineFactory.close() BEFORE sharedRMDCache.close() to ensure callbacks see null before native memory is freed.
sixpluszero
approved these changes
Apr 15, 2026
sixpluszero
left a comment
Contributor
There was a problem hiding this comment.
Thank you for the quick fix!
Contributor
Author
|
Thanks for the review, @sixpluszero ! |
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.
Summary
Fix JVM crashes (SIGSEGV) caused by use-after-free in RMD block cache metrics callbacks.
The RMD block cache async gauge callbacks captured
rmdCachedirectlyvia method references (
rmdCache::getUsage,rmdCache::getPinnedUsage)with no lifecycle guard. When
RocksDBStorageEngineFactory.close()freesthe native Cache object, the OTel/Tehuti callbacks still fire on the
next 60-second collection cycle, dereferencing the stale JNI handle and
causing a SIGSEGV.
This caused 21+ JVM crashes across EI and prod over 6 days. 82% of
crashes were on the
collectionWindowExecutor-1(OTel) orAsync_Gauge_Executor(Tehuti) metrics threads. The dominant crashsignature was a classic C++ vtable use-after-free: the freed Cache
memory was reused by the Java heap, so virtual method dispatch jumped
to garbage addresses containing Java class metadata.
The existing partition-level metrics (block-cache-capacity, etc.) are
safe because they go through
getRocksDBStatValue()which is guardedby
synchronized(hostedRocksDBPartitions)andreadCloseRWLock. TheRMD cache callbacks bypassed all of these guards.
Fix: Store the Cache reference in a volatile field. Callbacks read it
into a local variable before null-checking — the local pins the reference
on the stack so it cannot become null between check and JNI call (no
TOCTOU race, no locks needed).
closeRMDBlockCache()nulls the volatilereference, and is called from
RocksDBStorageEngineFactory.close()BEFOREsharedRMDCache.close()to ensure callbacks see null before native memoryis freed.
Testing Done
without requiring locks (same pattern used in JDK concurrent utilities)