Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile] Let background processing preempt an active refresh run

laurenspriemad5277bd9b1b
diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dart
index c891675b12..2acd7b5e5b 100644
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@ -246,6 +246,7 @@ Future<void> runBackgroundTask(
Duration? mlSelfStop,
Duration? mlLockWait,
MlRunControl? control,
+ bool Function()? shouldYield,
}) async {
// Created at task start so a stop that fires before ML begins stays
// latched for the whole task.
@@ -285,19 +286,36 @@ Future<void> runBackgroundTask(
"[BG TASK] No recent foreground activity, proceeding with background work",
);
- await _runMinimally(taskId, tlog, mlRunControl, mlLockWait);
+ await _runMinimally(
+ taskId,
+ tlog,
+ mlRunControl,
+ mlLockWait,
+ shouldYield ?? () => false,
+ );
} finally {
mlSelfStopTimer?.cancel();
mlForegroundWatchTimer.cancel();
}
}
+void stopBackgroundSync() {
+ if (_isSyncInitialized) SyncService.instance.stopSync();
+}
+
Future<void> _runMinimally(
String taskId,
TimeLogger tlog,
MlRunControl mlRunControl,
Duration? mlLockWait,
+ bool Function() shouldYield,
) async {
+ bool isYieldingBefore(String stage) {
+ if (!shouldYield()) return false;
+ _logger.info("[BG TASK] $taskId yielding before $stage");
+ return true;
+ }
+
try {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
final SharedPreferences prefs = await SharedPreferences.getInstance();
@@ -354,9 +372,11 @@ Future<void> _runMinimally(
_logger.info("[BG TASK] update notification");
updateService.showUpdateNotification().ignore();
+ if (isYieldingBefore("sync")) return;
_logger.info("[BG TASK] sync starting");
await _sync('bgTaskActiveProcess');
_logger.info("[BG TASK] sync completed");
+ if (isYieldingBefore("post-sync work")) return;
_logger.info("[BG TASK] locale fetch");
final locale = await getLocale();
@@ -403,6 +423,7 @@ Future<void> _runMinimally(
}
}
}
+ if (isYieldingBefore("smart albums sync")) return;
_logger.info("[BG TASK] smart albums sync");
await smartAlbumsService.syncSmartAlbums();
diff --git a/mobile/apps/photos/lib/utils/background_tasks.dart b/mobile/apps/photos/lib/utils/background_tasks.dart
index 8afe74040b..ef1c508bb6 100644
--- a/mobile/apps/photos/lib/utils/background_tasks.dart
+++ b/mobile/apps/photos/lib/utils/background_tasks.dart
@@ -27,6 +27,7 @@ class BackgroundTasks {
static final _logger = Logger("BackgroundTasks");
static const refresh = "io.ente.photos.nativeBackgroundRefresh";
static const processing = "io.ente.photos.nativeBackgroundProcessing";
+ static const _pipelineHandoffWait = Duration(seconds: 5);
static Future<void> _configuration = Future.value();
static bool? _configuredNative;
@@ -167,6 +168,24 @@ class BackgroundTasks {
}
}
+ static Future<bool> _acquirePipeline(BackgroundTask task) async {
+ final wait = task.identifier == processing
+ ? _pipelineHandoffWait
+ : Duration.zero;
+ final waited = Stopwatch()..start();
+ while (true) {
+ if (await ProcessLockClient.instance.tryAcquire(
+ name: "background_process",
+ origin: "bg",
+ operation: task.identifier,
+ )) {
+ return true;
+ }
+ if (task.isStopping || waited.elapsed >= wait) return false;
+ await Future<void>.delayed(const Duration(milliseconds: 500));
+ }
+ }
+
static Future<void> retireLegacySchedules() async {
BgTaskUtils.resetProcessingSchedule();
final identifiers = Platform.isIOS
@@ -198,12 +217,7 @@ class BackgroundTasks {
return;
}
task.throwIfStopping();
- final acquired = await ProcessLockClient.instance.tryAcquire(
- name: "background_process",
- origin: "bg",
- operation: task.identifier,
- );
- if (!acquired) {
+ if (!await _acquirePipeline(task)) {
_logger.info(
"${task.identifier}: skipped, background pipeline busy",
);
@@ -227,6 +241,9 @@ class BackgroundTasks {
? MlStopReason.foregroundActive
: MlStopReason.backgroundDeadline,
);
+ if (reason == BackgroundStopReason.preempted) {
+ stopBackgroundSync();
+ }
}),
);
try {
@@ -236,6 +253,8 @@ class BackgroundTasks {
taskName,
TimeLogger(),
control: control,
+ shouldYield: () =>
+ task.stopReason == BackgroundStopReason.preempted,
mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName) - task.elapsed,
mlLockWait: BgTaskUtils.mlLockWaitFor(taskName),
).timeout(
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.kt
index 6522e1e450..b6b6b1e881 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
@@ -35,6 +35,7 @@ internal object BackgroundRuntime {
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 const val PREEMPTION_TIMEOUT_MS = 30_000L
private val main = Handler(Looper.getMainLooper())
private val scheduler = Executors.newSingleThreadExecutor()
private val observers = LinkedHashSet<BackgroundManagerPlugin>()
@@ -44,6 +45,7 @@ internal object BackgroundRuntime {
private var isAllowed: () -> Boolean = { false }
private var isForeground = false
private var active: Run? = null
+ private var pending: Pending? = null
var createEngine: (Context) -> FlutterEngine = { FlutterEngine(it) }
var findCallback: (Long) -> FlutterCallbackInformation? =
FlutterCallbackInformation::lookupCallbackInformation
@@ -66,6 +68,15 @@ internal object BackgroundRuntime {
val stopResults = ArrayList<MethodChannel.Result>()
}
+ private class Pending(
+ val worker: BackgroundWorker,
+ val configuration: TaskConfiguration,
+ val startedAt: Long,
+ val completion: (Boolean) -> Unit,
+ ) {
+ var timer: Runnable? = null
+ }
+
fun install(app: Application, allowed: () -> Boolean) {
isAllowed = allowed
if (application != null) return
@@ -121,10 +132,12 @@ internal object BackgroundRuntime {
fun foreground() {
isForeground = true
+ skipPending("foreground")
active?.let { stop(it, "foreground") }
}
fun requestStop(result: MethodChannel.Result? = null) {
+ skipPending("requested")
val run = active
if (run == null) {
result?.success(null)
@@ -324,10 +337,12 @@ internal object BackgroundRuntime {
completion(false)
return@onMain
}
+ val occupant = active
+ val preempts = occupant != null && canPreempt(occupant, configuration)
val skip =
when {
!enabled -> "disabled"
- active != null -> "busy"
+ occupant != null && !preempts -> "busy"
isForeground -> "foreground"
else -> null
}
@@ -339,6 +354,10 @@ internal object BackgroundRuntime {
completion(true)
return@onMain
}
+ if (occupant != null) {
+ preempt(occupant, Pending(worker, configuration, startedAt, completion))
+ return@onMain
+ }
val run = Run(worker, configuration, startedAt, completion)
active = run
configuration.runBudgetMs?.let { budget ->
@@ -458,8 +477,48 @@ internal object BackgroundRuntime {
if (Looper.myLooper() == main.looper) action() else main.post { action() }
}
+ private fun canPreempt(occupant: Run, configuration: TaskConfiguration): Boolean =
+ pending == null &&
+ !isForeground &&
+ occupant.teardownFailure == null &&
+ occupant.configuration.kind == "refresh" &&
+ configuration.kind == "processing"
+
+ private fun preempt(occupant: Run, waiting: Pending) {
+ pending = waiting
+ waiting.timer =
+ Runnable { if (pending === waiting) skipPending("busy") }
+ .also { main.postDelayed(it, PREEMPTION_TIMEOUT_MS) }
+ stop(occupant, "preempted")
+ }
+
+ private fun takePending(): Pending? {
+ val waiting = pending ?: return null
+ pending = null
+ waiting.timer?.let(main::removeCallbacks)
+ return waiting
+ }
+
+ private fun skipPending(reason: String) {
+ val waiting = takePending() ?: return
+ report(waiting.configuration.identifier, "skipped", reason)
+ waiting.completion(true)
+ }
+
+ private fun resumePending() {
+ val waiting = takePending() ?: return
+ start(waiting.worker, waiting.startedAt, waiting.completion)
+ }
+
fun nativeStop(worker: BackgroundWorker) {
main.post {
+ pending
+ ?.takeIf { it.worker === worker }
+ ?.let {
+ takePending()
+ report(it.configuration.identifier, "stopped", "system")
+ it.completion(true)
+ }
active
?.takeIf { it.worker === worker }
?.let {
@@ -527,12 +586,14 @@ internal object BackgroundRuntime {
run.stopResults.forEach { it.error("teardown", failure.message, null) }
run.stopResults.clear()
run.completion(false)
+ resumePending()
return
}
if (terminal != "completed") report(run.configuration.identifier, terminal, reason, error)
run.completion(terminal != "failed" && terminal != "forcedTeardown")
run.stopResults.forEach { it.success(null) }
run.stopResults.clear()
+ resumePending()
}
private fun retireUnselectedSchedule(identifier: String) {
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.kt
index 6cac31a78c..008e9aece1 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
@@ -69,19 +69,26 @@ class BackgroundRuntimeTest {
awaitReply(beginConfigure(policy, enabled))
}
- private fun beginConfigure(policy: TaskConfiguration, enabled: Boolean = true): Reply {
- val data = org.json.JSONObject(policy.encode())
+ private fun configure(policies: List<TaskConfiguration>) {
+ awaitReply(beginConfigure(policies, enabled = true))
+ }
+
+ private fun beginConfigure(policy: TaskConfiguration, enabled: Boolean = true): Reply =
+ beginConfigure(listOf(policy), enabled)
+
+ private fun beginConfigure(policies: List<TaskConfiguration>, enabled: Boolean): Reply {
val result = Reply()
BackgroundRuntime.configure(
mapOf(
"enabled" to enabled,
"callbackHandle" to 1L,
"tasks" to
- listOf(
+ policies.map { policy ->
+ val data = org.json.JSONObject(policy.encode())
data.keys().asSequence().associateWith { key ->
data.get(key).takeUnless { it === org.json.JSONObject.NULL }
}
- ),
+ },
),
result,
)
@@ -410,6 +417,66 @@ class BackgroundRuntimeTest {
reconfigureWhileStarting(cooperative, enabled = true)
reconfigureWhileStarting(cooperative, enabled = false)
+
+ val processing = cooperative.copy(identifier = "test.processing", kind = "processing")
+ configure(listOf(cooperative, processing))
+ val yielding = worker(cooperative).startWork()
+ val yieldingChannel = bootstrap()
+ val yieldingReady = yieldingChannel.call("ready") as Map<*, *>
+ val enginesBeforePreemption = engines.size
+ val waiting = worker(processing).startWork()
+ assertFalse(waiting.isDone)
+ assertTrue(startups.isEmpty())
+ assertEquals(enginesBeforePreemption, engines.size)
+ assertEquals("preempted", (yieldingChannel.sent.single().arguments as Map<*, *>)["reason"])
+ assertEquals(ListenableWorker.Result.success(), worker(processing).startWork().get())
+ assertFalse(waiting.isDone)
+ complete(yieldingChannel, yieldingReady)
+ assertEquals(ListenableWorker.Result.success(), yielding.get())
+ val resumedChannel = bootstrap()
+ val resumedReady = resumedChannel.call("ready") as Map<*, *>
+ assertEquals(processing.identifier, resumedReady["identifier"])
+ assertNull(resumedReady["stopReason"])
+ assertEquals(ListenableWorker.Result.success(), worker(cooperative).startWork().get())
+ assertTrue(resumedChannel.sent.isEmpty())
+ complete(resumedChannel, resumedReady)
+ assertEquals(ListenableWorker.Result.success(), waiting.get())
+
+ val unyielding = worker(cooperative).startWork()
+ val unyieldingChannel = bootstrap()
+ val unyieldingReady = unyieldingChannel.call("ready") as Map<*, *>
+ val timedOut = worker(processing).startWork()
+ main.idleFor(Duration.ofSeconds(29))
+ assertFalse(timedOut.isDone)
+ main.idleFor(Duration.ofSeconds(2))
+ assertEquals(ListenableWorker.Result.success(), timedOut.get())
+ complete(unyieldingChannel, unyieldingReady)
+ assertTrue(unyielding.isDone)
+ assertTrue(startups.isEmpty())
+
+ val interruptedRefresh = worker(cooperative).startWork()
+ val interruptedChannel = bootstrap()
+ val interruptedReady = interruptedChannel.call("ready") as Map<*, *>
+ val abandoned = worker(processing).startWork()
+ assertFalse(abandoned.isDone)
+ val arriving = Robolectric.buildActivity(Activity::class.java).create().start().resume()
+ assertEquals(ListenableWorker.Result.success(), abandoned.get())
+ complete(interruptedChannel, interruptedReady)
+ assertTrue(interruptedRefresh.isDone)
+ assertTrue(startups.isEmpty())
+ arriving.pause().stop().destroy()
+
+ val stoppedRefresh = worker(cooperative).startWork()
+ val stoppedRefreshChannel = bootstrap()
+ val stoppedRefreshReady = stoppedRefreshChannel.call("ready") as Map<*, *>
+ val stoppedWhileWaitingWorker = worker(processing)
+ val stoppedWhileWaiting = stoppedWhileWaitingWorker.startWork()
+ stoppedWhileWaitingWorker.onStopped()
+ main.idle()
+ complete(stoppedRefreshChannel, stoppedRefreshReady)
+ assertTrue(stoppedRefresh.isDone)
+ assertTrue(startups.isEmpty())
+ assertEquals(ListenableWorker.Result.success(), stoppedWhileWaiting.get())
configure(cooperative)
val teardownWorker = worker(cooperative)
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.swift
index d17fdff380..f11fd711b2 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
@@ -8,12 +8,14 @@ import os
final class BackgroundRuntime: NSObject {
static let shared = BackgroundRuntime()
private static let logger = Logger(subsystem: "io.ente.background", category: "BackgroundManager")
+ private static let preemptionTimeout: TimeInterval = 30
private var configuration = StoredConfiguration.load()
private var isAllowed: () -> Bool = { false }
private var registrant: ((FlutterPluginRegistry) -> Void)?
private let observers = NSHashTable<BackgroundManagerPlugin>.weakObjects()
private var registrations: [String: Bool] = [:]
private var active: Run?
+ private var pending: Pending?
private var isForeground = false
private var installed = false
private var reconciling = false
@@ -41,6 +43,19 @@ final class BackgroundRuntime: NSObject {
}
}
+ private final class Pending {
+ let task: BGTask
+ let configuration: TaskConfiguration
+ let startedAt: TimeInterval
+ var timer: DispatchWorkItem?
+
+ init(task: BGTask, configuration: TaskConfiguration, startedAt: TimeInterval) {
+ self.task = task
+ self.configuration = configuration
+ self.startedAt = startedAt
+ }
+ }
+
func install(
isEnabled: @escaping () -> Bool,
registrant: @escaping (FlutterPluginRegistry) -> Void
@@ -90,6 +105,7 @@ final class BackgroundRuntime: NSObject {
@objc private func foreground() {
isForeground = true
+ finishPending(outcome: "skipped", reason: "foreground", success: true)
if let run = active { stop(run, reason: "foreground") }
}
@@ -98,6 +114,7 @@ final class BackgroundRuntime: NSObject {
}
func requestStop(result: FlutterResult? = nil) {
+ finishPending(outcome: "skipped", reason: "requested", success: true)
guard let run = active else {
result?(nil)
return
@@ -260,10 +277,12 @@ final class BackgroundRuntime: NSObject {
identifier: task.identifier, outcome: "failed", reason: "schedule",
error: String(describing: error))
}
+ let occupant = active
+ let preempts = occupant.map { canPreempt($0, with: policy) } ?? false
let skip: String?
if !isAllowed() {
skip = "disabled"
- } else if active != nil {
+ } else if occupant != nil, !preempts {
skip = "busy"
} else if isForeground || UIApplication.shared.applicationState != .background {
skip = "foreground"
@@ -275,15 +294,66 @@ final class BackgroundRuntime: NSObject {
task.setTaskCompleted(success: true)
return
}
- let run = Run(task: task, configuration: policy, startedAt: startedAt)
- active = run
- task.expirationHandler = { [weak run] in
+ task.expirationHandler = { [weak task] in
DispatchQueue.main.async {
- guard let run, self.active === run else { return }
- self.stop(run, reason: "system")
- self.retire(run, outcome: "stopped", reason: "expired", success: false)
+ guard let task else { return }
+ self.expire(task)
}
}
+ if let occupant {
+ preempt(occupant, for: Pending(task: task, configuration: policy, startedAt: startedAt))
+ return
+ }
+ admit(task, policy: policy, startedAt: startedAt)
+ }
+
+ private func expire(_ task: BGTask) {
+ if pending?.task === task {
+ finishPending(outcome: "stopped", reason: "expired", success: false)
+ } else if let run = active, run.task === task {
+ stop(run, reason: "system")
+ retire(run, outcome: "stopped", reason: "expired", success: false)
+ }
+ }
+
+ private func canPreempt(_ occupant: Run, with policy: TaskConfiguration) -> Bool {
+ pending == nil && !isForeground && UIApplication.shared.applicationState == .background
+ && occupant.configuration.kind == "refresh" && policy.kind == "processing"
+ }
+
+ private func preempt(_ occupant: Run, for waiting: Pending) {
+ pending = waiting
+ let timer = DispatchWorkItem { [weak waiting] in
+ guard let waiting, self.pending === waiting else { return }
+ self.finishPending(outcome: "skipped", reason: "busy", success: true)
+ }
+ waiting.timer = timer
+ DispatchQueue.main.asyncAfter(deadline: .now() + Self.preemptionTimeout, execute: timer)
+ stop(occupant, reason: "preempted")
+ }
+
+ private func takePending() -> Pending? {
+ guard let waiting = pending else { return nil }
+ pending = nil
+ waiting.timer?.cancel()
+ return waiting
+ }
+
+ private func finishPending(outcome: String, reason: String, success: Bool) {
+ guard let waiting = takePending() else { return }
+ report(identifier: waiting.configuration.identifier, outcome: outcome, reason: reason)
+ waiting.task.expirationHandler = nil
+ waiting.task.setTaskCompleted(success: success)
+ }
+
+ private func resumePending() {
+ guard let waiting = takePending() else { return }
+ admit(waiting.task, policy: waiting.configuration, startedAt: waiting.startedAt)
+ }
+
+ private func admit(_ task: BGTask, policy: TaskConfiguration, startedAt: TimeInterval) {
+ let run = Run(task: task, configuration: policy, startedAt: startedAt)
+ active = run
if let budget = policy.runBudgetMs {
let timer = DispatchWorkItem { [weak run] in
guard let run, self.active === run else { return }
@@ -400,6 +470,7 @@ final class BackgroundRuntime: NSObject {
}
for result in run.stopResults { result(nil) }
run.stopResults.removeAll()
+ resumePending()
}
private func report(
diff --git a/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart b/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart
index 73d4fc5b70..b7c247854a 100644
--- a/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart
+++ b/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart
@@ -6,7 +6,7 @@ import "package:flutter/widgets.dart";
enum BackgroundTaskKind { refresh, processing }
-enum BackgroundStopReason { foreground, budget, requested, system }
+enum BackgroundStopReason { foreground, budget, requested, preempted, system }
enum BackgroundTaskResult { completed, skipped, stopped, failed }