Repository navigation
Description
What is broken.
Signing out is meant to stop the periodic SyncWorker. SessionEffects.onSignedOut() cancels it, and its KDoc calls that the fix for a phone that «went on waking every fifteen minutes for the rest of the install's life». The cancel only lasts until the process dies.
LessonsApplication.observeSyncInterval() runs on every cold start. It collects the settings DataStore flow, which emits its current value straight away, and passes the interval to SyncScheduler.schedulePeriodic without checking whether the phone is in any class. The periodic worker is therefore back on the next cold start.
From then on it fires at the stored interval (60 minutes by default, as often as 15 if the user chose that). Each run wakes the device and needs a network constraint. It finds no session and returns Result.success(), so WorkManager keeps it scheduled. Each run is itself a cold start, so the worker keeps re-arming itself and never stops.
A fresh install that never joined a class gets the same worker from its first launch.
The failure scenario.
- A phone is in one class. The user leaves it, or signs out.
SessionRepositoryImpl.leave/signOut→SessionEffects.onSignedOut()→SyncScheduler.cancelPeriodic, and the unique worklessons-sync-periodicis cancelled. - The process dies. This happens when the app is swiped away, when Android reclaims memory, or when the phone reboots.
- Something starts the process again: the user opens the app on the join screen, the phone boots (the
BOOT_COMPLETEDreceivers in:core:dataand:widget), or the launcher updates a widget.LessonsApplication.onCreate()→observeSyncInterval()gets the firstsyncIntervalMinutes(60 unless changed) →SyncScheduler.schedulePeriodic(…, 60)→enqueueUniquePeriodicWork("lessons-sync-periodic", UPDATE, …). The worker is installed again. - Every 60 minutes while there is a network, WorkManager wakes the device and starts
SyncWorker.SyncWorkercallsGraph.init, seessessionRepository.current() == nulland returnsResult.success(). Each of these runs is a cold start, so step 3 repeats and the worker never goes away. - Result: a phone in no class wakes on the sync schedule for the life of the install, which is the exact behaviour
onSignedOutwas written to end.SessionEffectsTeststill passes, because it testsSessionEffectsalone and never runsLessonsApplication.
Where.
android/app/src/main/kotlin/com/lumenpearson/lessons/LessonsApplication.kt:41:observeSyncInterval()is called fromonCreate().android/app/src/main/kotlin/com/lumenpearson/lessons/LessonsApplication.kt:56-65: the collector callsSyncScheduler.schedulePeriodicwithout checking the session.android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/datastore/LessonsPreferences.kt:83:settingsis a DataStore flow that emits on first collection.android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/sync/SyncScheduler.kt:48-67:schedulePeriodicusesenqueueUniquePeriodicWork(…, UPDATE, …).android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/di/SessionEffects.kt:37-58:onSignedOut()cancels the worker, and its KDoc says the cancel is the fix.android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/di/LessonsContainer.kt:114:stopBackgroundSync→SyncScheduler.cancelPeriodic, the only caller of the cancel.android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/sync/SyncWorker.kt:36-38: with no session the worker returnsResult.success()at once, so the periodic work is never dropped.android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/repository/Models.kt:313: the default interval is 60 minutes; 15 is the floor (line 316).
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
Let a phone sign in to its own diary and hand our server only the ses…
Let a family in through its own school's diary at the first run, and …
Document the phone's half: the first run through your own school's di…