Skip to content

Quality: Bitmap cache uses non-thread-safe HashMap from potentially concurrent callers#2585

Merged
fire-light42 merged 1 commit intorecloudstream:masterfrom
Nam0101:contribai/improve/quality/bitmap-cache-uses-non-thread-safe-hashma
Mar 30, 2026
Merged

Quality: Bitmap cache uses non-thread-safe HashMap from potentially concurrent callers#2585
fire-light42 merged 1 commit intorecloudstream:masterfrom
Nam0101:contribai/improve/quality/bitmap-cache-uses-non-thread-safe-hashma

Conversation

@Nam0101
Copy link
Copy Markdown
Contributor

@Nam0101 Nam0101 commented Mar 29, 2026

Problem

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.

Severity: medium
File: app/src/main/java/com/lagradost/cloudstream3/utils/downloader/DownloadUtils.kt

Solution

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.

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

…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>
@fire-light42
Copy link
Copy Markdown
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.

@Nam0101
Copy link
Copy Markdown
Contributor Author

Nam0101 commented Mar 29, 2026

Thanks for the reminder. I used AI assistance for parts of this PR, and I’ll update the PR description to disclose that accordingly.

@fire-light42
Copy link
Copy Markdown
Collaborator

Thank you for your contribution 👍

@fire-light42 fire-light42 merged commit 736c637 into recloudstream:master Mar 30, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants