client: Guard SimpleCache construction and document cache key behaviour#6560
client: Guard SimpleCache construction and document cache key behaviour#6560VelikovPetar wants to merge 1 commit into
Conversation
- VideoMediaCache.create() now returns VideoMediaCache? and wraps SimpleCache construction in a try-catch; a stale directory lock from a prior crash no longer aborts ChatClient initialisation — video caching is silently disabled instead. - VideoCacheConfig KDoc notes that cache entries are keyed by URL path only (query parameters are stripped). - LRU eviction test comment acknowledges the wall-clock dependency of the Thread.sleep spacing and what to do if the test becomes flaky. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
PR checklist ❌The following issues were detected:
What we check
|
|
Closing — will be reopened via the correct workflow. |
Walkthrough
ChangesVideoMediaCache Nullable Creation
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/internal/VideoMediaCache.kt`:
- Around line 150-161: The VideoMediaCache.create path leaks the
StandaloneDatabaseProvider when SimpleCache construction fails, because the
provider is created successfully but never closed if the catch block is reached.
Move dbProvider handling so it is declared outside the try in
VideoMediaCache.create, and explicitly close it in the catch before returning
null; keep the existing logger.e message and ensure the cleanup covers the
failed SimpleCache(appContext, ...) construction path.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 3c2907f9-5498-4286-8bda-208c045a4198
📒 Files selected for processing (4)
stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/StreamCacheConfig.ktstream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/internal/VideoMediaCache.ktstream-chat-android-client/src/test/java/io/getstream/chat/android/client/cache/internal/StreamMediaDataSourceCacheIntegrationTest.ktstream-chat-android-client/src/test/java/io/getstream/chat/android/client/cache/internal/VideoMediaCacheTest.kt
| try { | ||
| val dbProvider = StandaloneDatabaseProvider(appContext) | ||
| val simpleCache = SimpleCache( | ||
| cacheDir, | ||
| LeastRecentlyUsedCacheEvictor(config.maxSizeBytes), | ||
| dbProvider, | ||
| ) | ||
| VideoMediaCache(simpleCache, dbProvider, key).also { instances[key] = it } | ||
| } catch (@Suppress("TooGenericExceptionCaught") e: Exception) { | ||
| logger.e(e) { "[create] Failed to construct SimpleCache at '$key'; video caching disabled." } | ||
| null | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Close StandaloneDatabaseProvider when SimpleCache construction fails.
If StandaloneDatabaseProvider(appContext) succeeds but SimpleCache(...) throws, dbProvider is leaked — it holds an open SQLite handle that is only closed in release(), which never runs for a failed create. Since the failed attempt is not registered in instances, a subsequent create for the same directory would open a second database provider while the first remains open.
🔧 Proposed fix: declare `dbProvider` outside the try and close it in the catch
- try {
- val dbProvider = StandaloneDatabaseProvider(appContext)
+ var dbProvider: StandaloneDatabaseProvider? = null
+ try {
+ dbProvider = StandaloneDatabaseProvider(appContext)
val simpleCache = SimpleCache(
cacheDir,
LeastRecentlyUsedCacheEvictor(config.maxSizeBytes),
dbProvider,
)
VideoMediaCache(simpleCache, dbProvider, key).also { instances[key] = it }
} catch (`@Suppress`("TooGenericExceptionCaught") e: Exception) {
+ dbProvider?.let {
+ try {
+ it.close()
+ } catch (`@Suppress`("TooGenericExceptionCaught") closeEx: Exception) {
+ logger.e(closeEx) { "[create] Failed to close StandaloneDatabaseProvider after cache construction failure" }
+ }
+ }
logger.e(e) { "[create] Failed to construct SimpleCache at '$key'; video caching disabled." }
null
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In
`@stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/internal/VideoMediaCache.kt`
around lines 150 - 161, The VideoMediaCache.create path leaks the
StandaloneDatabaseProvider when SimpleCache construction fails, because the
provider is created successfully but never closed if the catch block is reached.
Move dbProvider handling so it is declared outside the try in
VideoMediaCache.create, and explicitly close it in the catch before returning
null; keep the existing logger.e message and ensure the cleanup covers the
failed SimpleCache(appContext, ...) construction path.
SDK Size Comparison 📏
|
Summary
Backport of the same fixes applied to #6542 (port/v6-to-develop/opt-in-video-disk-cache).
VideoMediaCache.create()now returnsVideoMediaCache?— wrapsSimpleCacheconstruction in atry-catch. A stale directory lock left by a prior crash no longer abortsChatClientinitialisation; video caching is silently disabled instead.ChatClient.ktalready uses?.let {}so the nullable return is handled transparently.VideoCacheConfigKDoc — added one sentence noting that cache entries are keyed by URL path only (query parameters are stripped).Thread.sleepspacing and what to do if the test becomes flaky.Test plan
./gradlew :stream-chat-android-client:testDebugUnitTest --tests "*.cache.*"passes🤖 Generated with Claude Code
Summary by CodeRabbit