Modern Apps git

← Modern-Apps.git

emergency: serialize contact add and remove outcomes (87 tests)

Commit
7d7235e103613406ec8959659789195281d8f695
Author
Vayun Mathur <[email protected]>
When
2026-11-15 22:47 UTC (20 days ago)
Parents
1f044e9
'view model loads state and routes actions' flaked (reproduced
locally in about half of full-module runs, always at the await for
addFailed after adding an unresolvable contact). Diagnosis with a
direct repository call: the repository correctly rejects the URI and
returns false, yet state.addFailed stayed false. The valid add and
the invalid add run as independent Dispatchers.IO launches; the
first coroutine's post-repository assignment (addFailed = false)
could land after the second's (addFailed = true), overwriting it,
because nothing ordered the two outcomes.

Serialize addContact/removeContact through a Mutex in
EmergencyViewModel so each action's stored list and outcome publish
atomically in issue order; this is also the correct UI behaviour
for rapid add/remove taps.

Verified green 3x under TZ=UTC and TZ=America/New_York (the module
previously failed roughly one run in two).

Diff

emergency/src/main/java/com/vayunmathur/emergency/platform/EmergencyViewModel.kt
diff --git a/emergency/src/main/java/com/vayunmathur/emergency/platform/EmergencyViewModel.kt b/emergency/src/main/java/com/vayunmathur/emergency/platform/EmergencyViewModel.ktindex f6e04a59f4..8eea144325 100644--- a/emergency/src/main/java/com/vayunmathur/emergency/platform/EmergencyViewModel.kt+++ b/emergency/src/main/java/com/vayunmathur/emergency/platform/EmergencyViewModel.kt@@ -14,6 +14,8 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock  /** ViewModel backing the emergency info screens, combining stored info, grants and Health Connect state. */ class EmergencyViewModel(app: Application) : AndroidViewModel(app), EmergencyActions {@@ -27,6 +29,11 @@ class EmergencyViewModel(app: Application) : AndroidViewModel(app), EmergencyAct     private val ownerCandidates = MutableStateFlow<OwnerPickCandidates?>(null)     private val ownerPickFailed = MutableStateFlow(false) +    // Contact add/remove run as separate IO launches; without serialization+    // a slow add can publish its outcome after a later one, leaving+    // addFailed (and the stored list) describing the wrong action.+    private val contactMutex = Mutex()+     val state: StateFlow<EmergencyUiState> = combine(         repository.info,         loaded,@@ -126,13 +133,17 @@ class EmergencyViewModel(app: Application) : AndroidViewModel(app), EmergencyAct      override fun addContact(phoneUri: Uri) {         viewModelScope.launch(Dispatchers.IO) {-            addFailed.value = !repository.addContact(phoneUri)+            contactMutex.withLock {+                addFailed.value = !repository.addContact(phoneUri)+            }         }     }      override fun removeContact(phoneUri: Uri) {         viewModelScope.launch(Dispatchers.IO) {-            repository.removeContact(phoneUri)+            contactMutex.withLock {+                repository.removeContact(phoneUri)+            }         }     }