Serialize app-scope writes via Main.immediate (Codex hardening note)
Non-blocking follow-up from the audit: applicationScope ran on Dispatchers.Default, so two very fast conflicting persistence writes (recalibrate then immediately Reset) had no ordering guarantee. Main.immediate launches the coroutine body synchronously in call order, so the DataStore edits enqueue deterministically; DataStore still does the IO on its own dispatcher. Only lightweight persistence uses this scope. 64 tests passing; assembleDebug clean; runs on device. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,8 +22,13 @@ class AppContainer(context: Context) {
|
|||||||
* Outlives any single screen. Used for durable fire-and-forget persistence
|
* Outlives any single screen. Used for durable fire-and-forget persistence
|
||||||
* (e.g. saving a calibration): a screen-scoped coroutine would be cancelled if
|
* (e.g. saving a calibration): a screen-scoped coroutine would be cancelled if
|
||||||
* the user leaves the moment the write is launched.
|
* the user leaves the moment the write is launched.
|
||||||
|
*
|
||||||
|
* Main.immediate (not Default) so writes launched from UI callbacks enqueue in
|
||||||
|
* call order — two fast conflicting writes (recalibrate then Reset) then reach
|
||||||
|
* DataStore deterministically. Only lightweight persistence runs here; DataStore
|
||||||
|
* still performs the actual IO on its own dispatcher.
|
||||||
*/
|
*/
|
||||||
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
|
||||||
|
|
||||||
val sensorSource: SensorSource by lazy { AndroidSensorSource(appContext) }
|
val sensorSource: SensorSource by lazy { AndroidSensorSource(appContext) }
|
||||||
val settings: SettingsRepository by lazy { SettingsRepository(appContext) }
|
val settings: SettingsRepository by lazy { SettingsRepository(appContext) }
|
||||||
|
|||||||
Reference in New Issue
Block a user