launcher: serialize app-list refreshes so stale loads cannot win
- Commit
25934427e9df91d51ceeb61dfccd52848a720a83- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:59 UTC (29 days ago)
- Parents
d64b17a
Symptom: during Play Store update sessions the drawer can
resurrect a just-removed app or lose a just-added one until
some later package event, and reconciliation can run against
that stale list.
Root cause: LauncherAppsMonitor.refresh launched a new
coroutine per package event with no ordering. Each loadApps
binder walk takes variable time, so an older load could
publish to _apps (and fire onChanged, which triggers
reconciliation and seeding) after a newer one had already
published. Last writer was whichever load finished last,
not whichever event happened last.
Fix: serialize load + publish + notify under a Mutex, the
same pattern TvHomeViewModel already uses for its rebuilds
('package broadcasts burst, and last-writer must win'). The
load inside the lock reads the freshest package state, so
queued refreshes converge instead of competing.
Verification: existing LauncherAppsMonitorTest covers start,
refresh notification and stale-key reporting; no ordering
test is added because loadApps cannot be made to complete
out of order deterministically under Robolectric. Full
module verification runs at the end of the stack.Diff
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.ktindex efd38b31f6..cb3b2ee556 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherAppsMonitor.kt@@ -17,6 +17,8 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext /** One launchable activity, as the drawer and the home screen see it. */@@ -67,6 +69,17 @@ class LauncherAppsMonitor(context: Context, private val scope: CoroutineScope) { * install or removal. Null means "the list changed but no icon needs re-rasterising", which * spares the icon cache from being thrown away on every package event. */+ /**+ * Serializes refreshes. Package events burst during install+ * sessions, and each load takes a different amount of binder+ * time: without this, an older load can publish after a newer+ * one, resurrecting a removed app (or hiding an added one)+ * until the next package event, and reconciliation then runs+ * against that stale list. The TV ViewModel serializes its+ * rebuilds for the same reason.+ */+ private val refreshMutex = Mutex()+ private var onChanged: ((PackageKey?) -> Unit)? = null private val callback = object : LauncherApps.Callback() {@@ -149,10 +162,12 @@ class LauncherAppsMonitor(context: Context, private val scope: CoroutineScope) { */ fun refresh(stale: PackageKey? = null) { scope.launch {- val loaded = withContext(Dispatchers.IO) { loadApps() }- _apps.value = loaded- // Back on the caller's dispatcher - consumers update UI state from here.- onChanged?.invoke(stale)+ refreshMutex.withLock {+ val loaded = withContext(Dispatchers.IO) { loadApps() }+ _apps.value = loaded+ // Back on the caller's dispatcher - consumers update UI state from here.+ onChanged?.invoke(stale)+ } } }