π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile] Recover interrupted background schedule updates
laurenspriem7102a1404d72
diff --git a/mobile/apps/photos/plugins/ente_background_manager/README.md b/mobile/apps/photos/plugins/ente_background_manager/README.mdindex 185338f5e3..7e52a367b6 100644--- a/mobile/apps/photos/plugins/ente_background_manager/README.md+++ b/mobile/apps/photos/plugins/ente_background_manager/README.md@@ -16,7 +16,7 @@ The package is part of the mobile workspace. Photos does not depend on or initiaTask configuration supports refresh/processing kind, frequency, initial delay, supported native constraints, and two optional durations. `runBudget` requests cooperative stopping from native entry, including engine startup. `foregroundStopTimeout` starts force teardown after the first foreground arrival. Omission disables the corresponding timer; zero acts immediately and negative durations are rejected. Android supports periodic flex and device-idle constraints. iOS supports network/power constraints only for processing tasks; unsupported combinations are rejected.-iOS configurations accept at most one refresh task, including when scheduling is disabled. Configurations exceeding this limit fail before changing stored settings, pending schedules, or active work. [Apple limits each app to one pending refresh request and ten pending processing requests](https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler/submit(_:)).+iOS configurations accept at most one refresh task and ten processing tasks, including when scheduling is disabled. Configurations exceeding these limits fail before changing stored settings, pending schedules, or active work. [Apple limits each app to one pending refresh request and ten pending processing requests](https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler/submit(_:)).Before supplying `foregroundStopTimeout`, validate the lifetime of the consumer's native/FFI operations. Destroying a Flutter engine does not establish that those operations have stopped.@@ -24,7 +24,7 @@ Before supplying `foregroundStopTimeout`, validate the lifetime of the consumer'Each platform has one process-local runtime. Admission checks backend eligibility, the active execution slot, and native foreground visibility before creating an engine. Busy or foreground deliveries finish as skips. Every admitted run captures its configuration and dispatcher binding and uses a fresh engine. On Android, a stop received during Flutter initialization retires the run before creating an engine.-The native eligibility callback controls whether work may run; it preserves future schedules when eligibility is temporarily false. Scheduling follows the configured enablement and task identifiers. On iOS, failed schedule updates remain pending for a later configuration call or normal delivery to apply them; the plugin does not retry automatically.+The native eligibility callback controls whether work may run; it preserves future schedules when eligibility is temporarily false. Scheduling follows the configured enablement and task identifiers. iOS stores the last successfully submitted policy for each task, so a later configuration call or normal delivery can apply unfinished updates after a process restart. Older stored configurations without this submission record are reconciled once. Android tags its native requests so later configuration calls can find and cancel removed tasks even if an earlier cancellation was interrupted. Neither platform retries automatically.The slot remains occupied during startup, execution, cleanup, and teardown. Native callbacks and timers are tied to a unique invocation. Foreground entry always requests stopping, including during startup. Stops remain latched; repeated visibility changes cannot revive a task or extend its grace. A callback returning `completed` after a stop request is reported as `stopped`. Configuration updates affect later runs only.diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.ktindex e113649b0a..4d31232d68 100644--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt@@ -32,6 +32,8 @@ internal object BackgroundRuntime {private const val PREFERENCES = "ente_background_manager"private const val CONFIGURATION = "configuration"private const val TASK_DATA = "task"+ private const val WORK_TAG = "io.ente.background"+ private const val IDENTIFIER_TAG_PREFIX = "io.ente.background.task:"private val main = Handler(Looper.getMainLooper())private val scheduler = Executors.newSingleThreadExecutor()private val observers = LinkedHashSet<BackgroundManagerPlugin>()@@ -173,8 +175,21 @@ internal object BackgroundRuntime {val manager = WorkManager.getInstance(app)val selected =if (enabled) configurations.map { it.identifier }.toSet() else emptySet()+ val existingIdentifiers =+ manager+ .getWorkInfosByTag(WORK_TAG)+ .get()+ .filterNot { it.state.isFinished }+ .flatMap { it.tags }+ .filter { it.startsWith(IDENTIFIER_TAG_PREFIX) }+ .map { it.removePrefix(IDENTIFIER_TAG_PREFIX) }+ .toMutableSet()for (index in 0 until previousTasks.length()) {- val identifier = previousTasks.getJSONObject(index).getString("identifier")+ existingIdentifiers.add(+ previousTasks.getJSONObject(index).getString("identifier")+ )+ }+ for (identifier in existingIdentifiers) {if (identifier !in selected && identifier != activeIdentifier) {manager.cancelUniqueWork(identifier).result.get()}@@ -198,6 +213,8 @@ internal object BackgroundRuntime {)val request =builder+ .addTag(WORK_TAG)+ .addTag(IDENTIFIER_TAG_PREFIX + configuration.identifier).setInitialDelay(configuration.initialDelayMs,TimeUnit.MILLISECONDS,diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.ktindex a6dea827a6..73509c344b 100644--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt@@ -7,6 +7,8 @@ import android.os.SystemClockimport androidx.work.Configurationimport androidx.work.Dataimport androidx.work.ListenableWorker+import androidx.work.WorkInfo+import androidx.work.WorkManagerimport androidx.work.testing.TestListenableWorkerBuilderimport androidx.work.testing.WorkManagerTestInitHelperimport io.flutter.FlutterInjector@@ -313,6 +315,29 @@ class BackgroundRuntimeTest {complete(afterHandlerFailureChannel, afterHandlerFailureReady)assertEquals(ListenableWorker.Result.success(), afterHandlerFailure.get())+ val removedRun = worker(cooperative).startWork()+ val removedChannel = bootstrap()+ val removedReady = removedChannel.call("ready") as Map<*, *>+ val replacementPolicy = cooperative.copy(identifier = "test.replacement")+ configure(replacementPolicy)+ assertFalse(removedRun.isDone)+ assertTrue(removedChannel.sent.isEmpty())+ val manager = WorkManager.getInstance(app)+ assertTrue(+ manager.getWorkInfosForUniqueWork(cooperative.identifier).get().any {+ !it.state.isFinished+ }+ )+ complete(removedChannel, removedReady)+ assertEquals(ListenableWorker.Result.success(), removedRun.get())+ configure(replacementPolicy)+ assertTrue(+ manager.getWorkInfosForUniqueWork(cooperative.identifier).get().all {+ it.state == WorkInfo.State.CANCELLED+ }+ )+ configure(cooperative)+val teardownWorker = worker(cooperative)var teardownCompletions = 0BackgroundRuntime.start(teardownWorker, SystemClock.elapsedRealtime()) { success ->diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundSchedulingTest.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundSchedulingTest.ktnew file mode 100644index 0000000000..8ae5c1d8de--- /dev/null+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundSchedulingTest.kt@@ -0,0 +1,199 @@+package io.ente.background++import android.app.Application+import android.content.Context+import android.os.Looper+import androidx.work.Configuration+import androidx.work.Data+import androidx.work.ExistingPeriodicWorkPolicy+import androidx.work.Operation+import androidx.work.PeriodicWorkRequest+import androidx.work.WorkInfo+import androidx.work.impl.WorkManagerImpl+import androidx.work.testing.WorkManagerTestInitHelper+import com.google.common.util.concurrent.Futures+import io.flutter.plugin.common.MethodChannel+import java.util.concurrent.TimeUnit+import org.json.JSONArray+import org.json.JSONObject+import org.junit.Assert.*+import org.junit.Test+import org.junit.runner.RunWith+import org.mockito.Mockito.*+import org.robolectric.RobolectricTestRunner+import org.robolectric.RuntimeEnvironment+import org.robolectric.Shadows.shadowOf+import org.robolectric.annotation.Config+import org.robolectric.annotation.LooperMode++@RunWith(RobolectricTestRunner::class)+@Config(sdk = [29])+@LooperMode(LooperMode.Mode.PAUSED)+class BackgroundSchedulingTest {+ private val app: Application+ get() = RuntimeEnvironment.getApplication()++ private fun policy(identifier: String) =+ TaskConfiguration(+ identifier,+ "refresh",+ 900_000,+ 86_400_000,+ null,+ false,+ false,+ false,+ null,+ null,+ 1,+ )++ private fun configure(vararg tasks: TaskConfiguration): Reply {+ val result = Reply()+ BackgroundRuntime.configure(+ mapOf(+ "enabled" to true,+ "callbackHandle" to 1L,+ "tasks" to+ tasks.map { task ->+ val json = JSONObject(task.encode())+ json.keys().asSequence().associateWith { key ->+ json.get(key).takeUnless { it === JSONObject.NULL }+ }+ },+ ),+ result,+ )+ val deadline = System.nanoTime() + 10_000_000_000L+ while (!result.done && System.nanoTime() < deadline) {+ shadowOf(Looper.getMainLooper()).idle()+ Thread.sleep(5)+ }+ assertTrue("Configuration did not finish", result.done)+ return result+ }++ @Test+ fun removedSchedulesAreRecoveredFromWorkManager() {+ WorkManagerTestInitHelper.initializeTestWorkManager(app, Configuration.Builder().build())+ BackgroundRuntime.install(app) { true }+ val original = WorkManagerImpl.getInstance(app)+ val manager = spy(original)+ WorkManagerImpl.setDelegate(manager)+ try {+ val kept = policy("schedule.kept")+ val removed = policy("schedule.removed")+ assertNull(configure(kept, removed).error)+ val failure = mock(Operation::class.java)+ `when`(failure.result)+ .thenReturn(+ Futures.immediateFailedFuture(IllegalStateException("Cancellation failed"))+ )+ doReturn(failure).`when`(manager).cancelUniqueWork(removed.identifier)+ assertEquals("schedule", configure(kept).error)+ assertFalse(+ manager+ .getWorkInfosForUniqueWork(removed.identifier)+ .get()+ .single()+ .state+ .isFinished+ )+ doCallRealMethod().`when`(manager).cancelUniqueWork(removed.identifier)+ assertNull(configure(kept).error)+ assertEquals(+ WorkInfo.State.CANCELLED,+ manager.getWorkInfosForUniqueWork(removed.identifier).get().single().state,+ )++ val interrupted = policy("schedule.interrupted")+ assertNull(configure(kept, interrupted).error)+ val preferences =+ app.getSharedPreferences("ente_background_manager", Context.MODE_PRIVATE)+ val desired = JSONObject(preferences.getString("configuration", null)!!)+ desired.put("tasks", JSONArray(listOf(JSONObject(kept.encode()))))+ assertTrue(preferences.edit().putString("configuration", desired.toString()).commit())+ assertNull(configure(kept).error)+ assertEquals(+ WorkInfo.State.CANCELLED,+ manager.getWorkInfosForUniqueWork(interrupted.identifier).get().single().state,+ )++ val reselected = policy("schedule.reselected")+ assertNull(configure(kept, reselected).error)+ doReturn(failure).`when`(manager).cancelUniqueWork(reselected.identifier)+ assertEquals("schedule", configure(kept).error)+ doCallRealMethod().`when`(manager).cancelUniqueWork(reselected.identifier)+ clearInvocations(manager)+ assertNull(configure(kept, reselected).error)+ verify(manager, never()).cancelUniqueWork(reselected.identifier)+ assertFalse(+ manager+ .getWorkInfosForUniqueWork(reselected.identifier)+ .get()+ .single()+ .state+ .isFinished+ )++ val untagged = policy("schedule.untagged")+ fun enqueue(task: TaskConfiguration) {+ val request =+ PeriodicWorkRequest.Builder(BackgroundWorker::class.java, 15, TimeUnit.MINUTES)+ .setInitialDelay(1, TimeUnit.DAYS)+ .setInputData(Data.Builder().putString("task", task.encode()).build())+ .build()+ manager+ .enqueueUniquePeriodicWork(+ task.identifier,+ ExistingPeriodicWorkPolicy.UPDATE,+ request,+ )+ .result+ .get()+ }+ enqueue(untagged)+ val previous = JSONObject(preferences.getString("configuration", null)!!)+ previous.put(+ "tasks",+ JSONArray(listOf(kept, reselected, untagged).map { JSONObject(it.encode()) }),+ )+ assertTrue(preferences.edit().putString("configuration", previous.toString()).commit())+ val unrelated = policy("schedule.unrelated")+ enqueue(unrelated)+ assertNull(configure(kept).error)+ assertEquals(+ WorkInfo.State.CANCELLED,+ manager.getWorkInfosForUniqueWork(untagged.identifier).get().single().state,+ )+ assertFalse(+ manager+ .getWorkInfosForUniqueWork(unrelated.identifier)+ .get()+ .single()+ .state+ .isFinished+ )+ } finally {+ WorkManagerImpl.setDelegate(original)+ }+ }++ private class Reply : MethodChannel.Result {+ var done = false+ var error: String? = null++ override fun success(result: Any?) {+ assertFalse(done)+ done = true+ }++ override fun error(code: String, message: String?, details: Any?) {+ assertFalse(done)+ error = code+ done = true+ }++ override fun notImplemented() = fail("Unexpected method")+ }+}diff --git a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swiftindex aa5a11f3a0..ead74d15a7 100644--- a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift+++ b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift@@ -18,7 +18,6 @@ final class BackgroundRuntime: NSObject {private var installed = falseprivate var reconciling = falseprivate var scheduleRevision = 0- private var changedTasks = Set<String>()private var configurationResults: [FlutterResult] = []private final class Run {@@ -134,11 +133,18 @@ final class BackgroundRuntime: NSObject {code: "configuration", message: "iOS supports at most one refresh task", details: nil))return}- let next = StoredConfiguration(enabled: enabled, tasks: parsed)- try next.save()- for task in parsed where !configuration.tasks.contains(task) {- changedTasks.insert(task.identifier)+ guard parsed.filter({ $0.kind == "processing" }).count <= 10 else {+ result(+ FlutterError(+ code: "configuration", message: "iOS supports at most ten processing tasks",+ details: nil))+ return}+ let identifiers = enabled ? Set(parsed.map(\.identifier)) : []+ let next = StoredConfiguration(+ enabled: enabled, tasks: parsed,+ submitted: configuration.submitted?.filter { identifiers.contains($0.identifier) })+ try next.save()configuration = nextif !enabled { requestStop() }configurationResults.append(result)@@ -166,15 +172,15 @@ final class BackgroundRuntime: NSObject {for identifier in self.registrations.keys where !identifiers.contains(identifier) {BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: identifier)}- self.changedTasks.formIntersection(identifiers)var failure: Error?for task in desired {- if pending[task.identifier] != nil && !self.changedTasks.contains(task.identifier) {+ if pending[task.identifier] != nil && (self.configuration.submitted ?? []).contains(task)+ {continue}do {try self.submit(task, delayMs: task.initialDelayMs)- self.changedTasks.remove(task.identifier)+ try self.recordSubmitted(task)} catch {failure = errorself.report(@@ -211,6 +217,14 @@ final class BackgroundRuntime: NSObject {try BGTaskScheduler.shared.submit(request)}+ private func recordSubmitted(_ task: TaskConfiguration) throws {+ if (configuration.submitted ?? []).contains(task) { return }+ var next = configuration+ next.submitted = (next.submitted ?? []).filter { $0.identifier != task.identifier } + [task]+ try next.save()+ configuration = next+ }+func scheduledTasks(_ result: @escaping FlutterResult) {BGTaskScheduler.shared.getPendingTaskRequests { requests inDispatchQueue.main.async {@@ -236,7 +250,7 @@ final class BackgroundRuntime: NSObject {}do {try submit(policy, delayMs: policy.frequencyMs)- changedTasks.remove(task.identifier)+ try recordSubmitted(policy)} catch {report(identifier: task.identifier, outcome: "failed", reason: "schedule",diff --git a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/TaskConfiguration.swift b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/TaskConfiguration.swiftindex 34f583b14a..6e217ea66a 100644--- a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/TaskConfiguration.swift+++ b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/TaskConfiguration.swift@@ -57,6 +57,7 @@ struct TaskConfiguration: Codable, Equatable {struct StoredConfiguration: Codable {var enabled: Boolvar tasks: [TaskConfiguration]+ var submitted: [TaskConfiguration]? = nilstatic func load() -> StoredConfiguration {guard let data = UserDefaults.standard.data(forKey: "ente_background_manager"),