Skip to content

client: Add VideoMediaCache create/clear guards and document cache key behaviour#6561

Merged
andremion merged 4 commits into
v6from
fix/video-cache-simplecache-guard-v6
Jul 15, 2026
Merged

client: Add VideoMediaCache create/clear guards and document cache key behaviour#6561
andremion merged 4 commits into
v6from
fix/video-cache-simplecache-guard-v6

Conversation

@VelikovPetar

@VelikovPetar VelikovPetar commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Goal

Backport of the review fixes from #6542 to v6.

Guard against a crash during ChatClient initialisation when the SimpleCache
directory lock is stale (e.g. left by a prior process crash), and clarify
cache key semantics in the public docs.

Implementation

  • VideoMediaCache.create() now returns VideoMediaCache? and wraps
    SimpleCache construction in a try-catch. A stale directory lock no
    longer aborts ChatClient initialisation — video caching is silently
    disabled instead. ChatClient.kt already uses ?.let {} so the nullable
    return is handled transparently.
  • VideoCacheConfig KDoc: added a note that cache entries are keyed by URL
    path only (query parameters are stripped).
  • LRU eviction test: comment extended to acknowledge the wall-clock
    dependency of Thread.sleep spacing and what to do if the test flakes.

UI Changes

No UI changes.

Testing

./gradlew :stream-chat-android-client:testDebugUnitTest --tests "*.cache.*"

Summary by CodeRabbit

  • Bug Fixes

    • Improved video cache clearing to avoid disrupting active video playback.
    • Ensured unused video cache directories are safely removed and recreated when needed.
    • Improved handling of video cache initialization failures to prevent crashes.
  • Documentation

    • Clarified that cache entries are shared by URLs with different query parameters when their paths match.

- 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>
@VelikovPetar VelikovPetar added the pr:improvement Improvement label Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled (or ignored for dependabot PRs).

🎉 Great job! This PR is ready for review.

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.26 MB 5.32 MB 0.05 MB 🟢
stream-chat-android-offline 5.49 MB 5.54 MB 0.04 MB 🟢
stream-chat-android-ui-components 10.64 MB 10.75 MB 0.11 MB 🟢
stream-chat-android-compose 12.87 MB 12.96 MB 0.09 MB 🟢

VelikovPetar and others added 3 commits July 14, 2026 13:30
`clearCacheAndTemporaryFiles` decided whether to delete the video cache
directory based on `VideoMediaCache.clearAll()`, a process-wide boolean that
does not line up with the single directory being deleted. When it reported no
live cache, the fallback `deleteRecursively()` could run after another thread
created a new `SimpleCache` for the same directory, corrupting Media3's on-disk
index and lock.

Replace `clearAll()` with `clearOrDelete(cacheDir, deleteOnDisk)`, which decides
per directory under the registry lock: clear a live cache in place, or delete on
disk when none owns it. Holding the lock across the check and the delete keeps a
concurrent `create()` from registering a cache mid-delete. This also decouples
the cleanup test from global registry state.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
In VideoMediaCache.create, the StandaloneDatabaseProvider is opened before the
SimpleCache is constructed. When construction throws (e.g. a stale directory
lock), the catch returned null without closing the provider, leaking its
database connection on that path. Close it in the catch, guarded like release()
does, since close() can itself throw.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
73.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@VelikovPetar VelikovPetar changed the title client: Guard SimpleCache construction and document cache key behaviour client: Add VideoMediaCache create/clear guards and document cache key behaviour Jul 15, 2026
@VelikovPetar
VelikovPetar marked this pull request as ready for review July 15, 2026 09:38
@VelikovPetar
VelikovPetar requested a review from a team as a code owner July 15, 2026 09:38
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f65cad7a-6f58-413b-b4ca-4dce0252efd3

📥 Commits

Reviewing files that changed from the base of the PR and between c1a0352 and 6c122d8.

📒 Files selected for processing (5)
  • stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt
  • stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/StreamCacheConfig.kt
  • stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/internal/VideoMediaCache.kt
  • stream-chat-android-client/src/test/java/io/getstream/chat/android/client/cache/internal/StreamMediaDataSourceCacheIntegrationTest.kt
  • stream-chat-android-client/src/test/java/io/getstream/chat/android/client/cache/internal/VideoMediaCacheTest.kt

Walkthrough

Video cache clearing now atomically chooses between clearing a live cache and deleting an unowned directory. Cache creation returns null on initialization failure, and tests cover the updated lifecycle and cache-key documentation.

Changes

Video cache lifecycle

Layer / File(s) Summary
Lock-aware clearing and nullable creation
stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/internal/VideoMediaCache.kt, stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt
Adds registry-locked clearOrDelete, makes cache creation nullable on initialization failure, and updates client cache clearing to use the new flow.
Cache contract and lifecycle validation
stream-chat-android-client/src/main/java/io/getstream/chat/android/client/cache/StreamCacheConfig.kt, stream-chat-android-client/src/test/java/io/getstream/chat/android/client/cache/internal/*
Documents URL-path cache keys and tests live clearing, disk deletion, cache recreation, nullable creation, and eviction setup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: andremion

Poem

I’m a rabbit guarding cached streams,
With tidy paths and safer dreams.
Live caches clear, old files flee,
Failed starts return quietly.
Hop, hop—the locks now keep things right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main changes: VideoMediaCache guards and cache-key documentation.
Description check ✅ Passed The description covers Goal, Implementation, UI Changes, and Testing, with only non-critical template sections omitted.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/video-cache-simplecache-guard-v6

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@andremion
andremion merged commit 12c4e3a into v6 Jul 15, 2026
20 of 21 checks passed
@andremion
andremion deleted the fix/video-cache-simplecache-guard-v6 branch July 15, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:improvement Improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants