Modern Apps git

← Modern-Apps.git

camera: lens persists are serialized, and the zoom-stop suite runs quarantined

Commit
5102f885e353abbadcd38d03526549ef192b114a
Author
Vayun Mathur <[email protected]>
When
2026-12-01 05:37 UTC (5 days ago)
Parents
fdc0030
ZoomStopLensSyncTest failed only in the full suite ('stored
camera_last_lens_id never became 2') for two compounding
reasons. (1) Test pollution: CameraViewModel's init runs
over the process-wide DataStoreUtils file and launches
never-cleared collectors/persists; camera/build.gradle.kts
already quarantines every such class into
testDevUnitTestNative (forkEvery = 1) per its own audit
rule, but ZoomStopLensSyncTest was missing from
isolatedClasses โ€” batched neighbours' stale startup-lens
persists could land after the tele persist with nothing to
rewrite it. Passes alone because no foreign VMs share the
fork. (2) A real product race: persistStartupLens wrote the
facing and id as two separate suspending edits; back-to-back
persists (the 1x->2x sequence, or a startup-seed persist
racing a lens tap) could interleave at the suspension
points, persisting a stale id or a torn facing/id pair as
the startup lens.

The persist pair is now serialized under a process-wide
Mutex (process-wide because the DataStore is), so persists
complete in launch order and never tear; and the test class
joined isolatedClasses, with no assertion weakened.
Verified: the quarantined suite 4/4 green, the full
:camera:testDevUnitTest 887 tests (638 main + 249 native)
with 0 failures twice including a --rerun-tasks pass, and
:camera:lint BUILD SUCCESSFUL.

Diff

camera/build.gradle.kts
diff --git a/camera/build.gradle.kts b/camera/build.gradle.ktsindex 5548719c61..113a5e4acb 100644--- a/camera/build.gradle.kts+++ b/camera/build.gradle.kts@@ -121,6 +121,7 @@ rustNativeTests {             "com.vayunmathur.camera.platform.SessionLaddersExtraTest",             "com.vayunmathur.camera.platform.SingleCaptureNightTest",             "com.vayunmathur.camera.platform.SloMoLogicExtraTest",+            "com.vayunmathur.camera.platform.ZoomStopLensSyncTest",             "com.vayunmathur.camera.platform.ZslGatingTest",             "com.vayunmathur.camera.ui.BottomControlsComposeExtraTest",             "com.vayunmathur.camera.ui.CameraBindFailedComposeTest",
camera/src/main/java/com/vayunmathur/camera/platform/LensSelector.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/platform/LensSelector.kt b/camera/src/main/java/com/vayunmathur/camera/platform/LensSelector.ktindex 77eb9615fa..6661e2c2f3 100644--- a/camera/src/main/java/com/vayunmathur/camera/platform/LensSelector.kt+++ b/camera/src/main/java/com/vayunmathur/camera/platform/LensSelector.kt@@ -11,6 +11,8 @@ import com.vayunmathur.camera.domain.LensType import com.vayunmathur.camera.domain.PhysicalLens import com.vayunmathur.camera.platform.CameraViewModel import kotlinx.coroutines.launch+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock import kotlin.math.sqrt  /** Maps the pure [LensFacing] to the platform lens-facing constant (CameraMetadata values). */@@ -244,11 +246,24 @@ fun CameraViewModel.seedStartupLens() {     ) } +/**+ * Serializes startup-lens persists: facing and id are two separate+ * suspending DataStore edits, so without this lock back-to-back persists+ * (a zoom scrub landing on two stops in quick succession, a startup-seed+ * persist racing a lens tap) can interleave and resume out of order,+ * leaving the stale lens's id โ€” or a torn facing/id pair from two+ * different lenses โ€” as the persisted startup lens. Process-wide because+ * the underlying DataStore is a process-wide singleton.+ */+private val startupLensPersistMutex = Mutex()+ /** Persists [lens] as the startup fast-path lens for the next launch. */ fun CameraViewModel.persistStartupLens(lens: PhysicalLens) {     viewModelScope.launch {-        ds.setString(LAST_LENS_FACING_KEY, lens.facing.name)-        ds.setString(LAST_LENS_ID_KEY, lens.logicalCameraId)+        startupLensPersistMutex.withLock {+            ds.setString(LAST_LENS_FACING_KEY, lens.facing.name)+            ds.setString(LAST_LENS_ID_KEY, lens.logicalCameraId)+        }     } }