From 8ad2b4a3649e532aa2a8c0a2f0c32f47b55dd7ae Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 05:55:39 +0000 Subject: [PATCH] app: serialize superuser grant writes Each tap on the grant screen started its own database write on the IO dispatcher, with nothing ordering them. A grant takes two queries (fetch, then replace) while a revoke takes one, so a quick grant and revoke could run as fetch, delete, replace and leave the app granted while it was shown as not granted. The new state was also computed from the item the UI captured, so two taps before a recomposition both granted. Write in tap order under a Mutex, like SuperuserViewModel does, and toggle the current state of the app. Assisted-by: Claude Opus 5.5 --- .../ui/superuser/SuperuserGrantViewModel.kt | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserGrantViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserGrantViewModel.kt index 0b20b69e9..a956b7c72 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserGrantViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserGrantViewModel.kt @@ -28,6 +28,8 @@ import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.toCollection import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import timber.log.Timber import java.util.Locale @@ -63,6 +65,8 @@ class SuperuserGrantViewModel( private val policyDB: PolicyDao ) : AsyncLoadViewModel() { + private val writeLock = Mutex() + private val _loading = MutableStateFlow(true) val loading: StateFlow = _loading.asStateFlow() @@ -133,10 +137,14 @@ class SuperuserGrantViewModel( fun toggleSortReverse() { _sortReverse.value = !_sortReverse.value } fun toggleGrant(app: SuGrantAppState) { - val willGrant = !app.isGranted + // Toggle the current state rather than the one the UI captured: two quick taps can + // deliver the same item before it recomposes + val uid = app.info.uid + val target = _allApps.value.firstOrNull { it.info.uid == uid } ?: return + val willGrant = !target.isGranted _allApps.update { apps -> apps.map { current -> - if (current.info.uid == app.info.uid) { + if (current.info.uid == uid) { current.copy(isGranted = willGrant) } else { current @@ -145,14 +153,18 @@ class SuperuserGrantViewModel( } viewModelScope.launch { try { - withContext(Dispatchers.IO) { - if (willGrant) { - val existing = policyDB.fetch(app.info.uid) - val suPolicy = existing ?: SuPolicy(app.info.uid) - suPolicy.policy = if (Config.suRestrict) SuPolicy.RESTRICT else SuPolicy.ALLOW - policyDB.update(suPolicy) - } else { - policyDB.delete(app.info.uid) + // Write in tap order: a grant takes two queries and a revoke one, so unordered + // writes could leave an app granted while it is shown as not granted + writeLock.withLock { + withContext(Dispatchers.IO) { + if (willGrant) { + val existing = policyDB.fetch(uid) + val suPolicy = existing ?: SuPolicy(uid) + suPolicy.policy = if (Config.suRestrict) SuPolicy.RESTRICT else SuPolicy.ALLOW + policyDB.update(suPolicy) + } else { + policyDB.delete(uid) + } } } } catch (e: Exception) { @@ -160,7 +172,7 @@ class SuperuserGrantViewModel( Timber.e(e) _allApps.update { apps -> apps.map { current -> - if (current.info.uid == app.info.uid && current.isGranted == willGrant) { + if (current.info.uid == uid && current.isGranted == willGrant) { current.copy(isGranted = !willGrant) } else { current