Skip to content

A phone in no class has its periodic background sync re-armed on every cold start, undoing the sign-out cancel #152

Closed

Description

@lumenpearson

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.

  1. A phone is in one class. The user leaves it, or signs out. SessionRepositoryImpl.leave/signOut → SessionEffects.onSignedOut() → SyncScheduler.cancelPeriodic, and the unique work lessons-sync-periodic is cancelled.
  2. The process dies. This happens when the app is swiped away, when Android reclaims memory, or when the phone reboots.
  3. Something starts the process again: the user opens the app on the join screen, the phone boots (the BOOT_COMPLETED receivers in :core:data and :widget), or the launcher updates a widget. LessonsApplication.onCreate() → observeSyncInterval() gets the first syncIntervalMinutes (60 unless changed) → SyncScheduler.schedulePeriodic(…, 60) → enqueueUniquePeriodicWork("lessons-sync-periodic", UPDATE, …). The worker is installed again.
  4. Every 60 minutes while there is a network, WorkManager wakes the device and starts SyncWorker. SyncWorker calls Graph.init, sees sessionRepository.current() == null and returns Result.success(). Each of these runs is a cold start, so step 3 repeats and the worker never goes away.
  5. Result: a phone in no class wakes on the sync schedule for the life of the install, which is the exact behaviour onSignedOut was written to end. SessionEffectsTest still passes, because it tests SessionEffects alone and never runs LessonsApplication.

Where.

  • android/app/src/main/kotlin/com/lumenpearson/lessons/LessonsApplication.kt:41: observeSyncInterval() is called from onCreate().
  • android/app/src/main/kotlin/com/lumenpearson/lessons/LessonsApplication.kt:56-65: the collector calls SyncScheduler.schedulePeriodic without checking the session.
  • android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/datastore/LessonsPreferences.kt:83: settings is a DataStore flow that emits on first collection.
  • android/core/data/src/main/kotlin/com/lumenpearson/lessons/core/data/sync/SyncScheduler.kt:48-67: schedulePeriodic uses enqueueUniquePeriodicWork(…, 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 returns Result.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

No one assigned

    Type

    No type

    Projects

    • Status
      Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions