Quality: Bitmap cache uses non-thread-safe HashMap from potentially concurrent callers#2585
Merged
fire-light42 merged 1 commit intorecloudstream:masterfrom Mar 30, 2026
Conversation
…concurrent callers `cachedBitmaps` is a global mutable `HashMap` accessed without synchronization in `getImageBitmapFromUrl`. This utility is likely called from multiple coroutine/background contexts. Concurrent read/write on `HashMap` is unsafe and can lead to race conditions, stale reads, or map corruption/crashes under load. Affected files: DownloadUtils.kt Signed-off-by: Nguyen Van Nam <nam.nv205106@gmail.com>
Collaborator
|
Remember to mark your pull requests as AI assisted as per our AI Policy if you used AI for anything in the pull request. |
Contributor
Author
|
Thanks for the reminder. I used AI assistance for parts of this PR, and I’ll update the PR description to disclose that accordingly. |
Collaborator
|
Thank you for your contribution 👍 |
fire-light42
approved these changes
Mar 30, 2026
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
cachedBitmapsis a global mutableHashMapaccessed without synchronization ingetImageBitmapFromUrl. This utility is likely called from multiple coroutine/background contexts. Concurrent read/write onHashMapis unsafe and can lead to race conditions, stale reads, or map corruption/crashes under load.Severity:
mediumFile:
app/src/main/java/com/lagradost/cloudstream3/utils/downloader/DownloadUtils.ktSolution
Replace with a thread-safe map and atomic put:
private val cachedBitmaps = ConcurrentHashMap<String, Bitmap>()Then use:cachedBitmaps[cacheKey]?.let { return@safe it }...bitmap?.let { cachedBitmaps.putIfAbsent(cacheKey, it) }Changes
app/src/main/java/com/lagradost/cloudstream3/utils/downloader/DownloadUtils.kt(modified)AI usage
This pull request was partially AI-assisted. AI was used to help with code drafting, refactoring suggestions, and PR text.
Testing
I personally reviewed all generated suggestions, verified the logic, and tested the final implementation before opening this PR.