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
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 6699202d35
commit 8ad2b4a364
@@ -28,6 +28,8 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.toCollection import kotlinx.coroutines.flow.toCollection
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.util.Locale import java.util.Locale
@@ -63,6 +65,8 @@ class SuperuserGrantViewModel(
private val policyDB: PolicyDao private val policyDB: PolicyDao
) : AsyncLoadViewModel() { ) : AsyncLoadViewModel() {
private val writeLock = Mutex()
private val _loading = MutableStateFlow(true) private val _loading = MutableStateFlow(true)
val loading: StateFlow<Boolean> = _loading.asStateFlow() val loading: StateFlow<Boolean> = _loading.asStateFlow()
@@ -133,10 +137,14 @@ class SuperuserGrantViewModel(
fun toggleSortReverse() { _sortReverse.value = !_sortReverse.value } fun toggleSortReverse() { _sortReverse.value = !_sortReverse.value }
fun toggleGrant(app: SuGrantAppState) { 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 -> _allApps.update { apps ->
apps.map { current -> apps.map { current ->
if (current.info.uid == app.info.uid) { if (current.info.uid == uid) {
current.copy(isGranted = willGrant) current.copy(isGranted = willGrant)
} else { } else {
current current
@@ -145,14 +153,18 @@ class SuperuserGrantViewModel(
} }
viewModelScope.launch { viewModelScope.launch {
try { try {
withContext(Dispatchers.IO) { // Write in tap order: a grant takes two queries and a revoke one, so unordered
if (willGrant) { // writes could leave an app granted while it is shown as not granted
val existing = policyDB.fetch(app.info.uid) writeLock.withLock {
val suPolicy = existing ?: SuPolicy(app.info.uid) withContext(Dispatchers.IO) {
suPolicy.policy = if (Config.suRestrict) SuPolicy.RESTRICT else SuPolicy.ALLOW if (willGrant) {
policyDB.update(suPolicy) val existing = policyDB.fetch(uid)
} else { val suPolicy = existing ?: SuPolicy(uid)
policyDB.delete(app.info.uid) suPolicy.policy = if (Config.suRestrict) SuPolicy.RESTRICT else SuPolicy.ALLOW
policyDB.update(suPolicy)
} else {
policyDB.delete(uid)
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
@@ -160,7 +172,7 @@ class SuperuserGrantViewModel(
Timber.e(e) Timber.e(e)
_allApps.update { apps -> _allApps.update { apps ->
apps.map { current -> 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) current.copy(isGranted = !willGrant)
} else { } else {
current current