Skip to content

Cast: Fix thread safety and memory leak in CastMediaRouteProvider#3354

Open
koczadly wants to merge 3 commits intomicrog:masterfrom
koczadly:fix/cast-concurrency-exception
Open

Cast: Fix thread safety and memory leak in CastMediaRouteProvider#3354
koczadly wants to merge 3 commits intomicrog:masterfrom
koczadly:fix/cast-concurrency-exception

Conversation

@koczadly
Copy link

@koczadly koczadly commented Mar 22, 2026

Problem

CastMediaRouteProvider has no thread synchronisation for its shared mutable state, which can be accessed across numerous threads (NSD callbacks + main thread).
This has been causing ConcurrentModificationException crashes, which seems to be consistently reproducible whenever opening the Google Home app.

Stacktrace
java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1603)
	at java.util.HashMap$ValueIterator.next(HashMap.java:1631)
	at org.microg.gms.cast.CastMediaRouteProvider.publishRoutes(CastMediaRouteProvider.java:329)
	at org.microg.gms.cast.CastMediaRouteProvider.access$500(CastMediaRouteProvider.java:54)
	at org.microg.gms.cast.CastMediaRouteProvider$2.run(CastMediaRouteProvider.java:322)
	at android.os.Handler.handleCallback(Handler.java:995)
	at android.os.Handler.dispatchMessage(Handler.java:103)
	at android.os.Looper.loopOnce(Looper.java:265)
	at android.os.Looper.loop(Looper.java:358)
	at android.app.ActivityThread.main(ActivityThread.java:10049)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:616)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1115)

The inner customCategories collection is also appended to for every discovery request, causing it to grow indefinitely.

Changes

  • Add synchronisation by introducing a shared lock object to guard shared state
  • Fix unbounded category growth by switching to a Set and replacing contents on each discovery request
  • Minor code cleanup

Testing

Just to note, I have the ReVanced version installed on my device as I do not have root-level access. I understand their fork has a few minor differences, though this specific implementation appears to be completely unchanged.

I have deployed these changes onto my personal device and it has been working as expected.

Issues

Fixes #2449

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.

Minor chromecast ConcurrentModificationException

1 participant