Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] Wait for the ml lock in iOS background processing windows

laurenspriemcf10c48a8375
diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dart
index 43acc75336..18f3db6175 100644
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@ -83,6 +83,9 @@ const kBGPushTimeout = Duration(seconds: 28);
// leaving margin to drain in-flight work and release the process lock cleanly.
const kBGTaskMLSelfStopIOS = Duration(seconds: 26);
const kBGProcessingTaskMLSelfStopIOS = Duration(minutes: 3, seconds: 45);
+// A suspended main engine can still hold the ml lock when a processing window
+// opens; it drains its latched stop within seconds once the process is woken.
+const kBGProcessingTaskMLLockWaitIOS = Duration(seconds: 60);
const kBGTaskMLSelfStopAndroid = Duration(minutes: 9);
bool isProcessBg = true;
bool _stopHearBeat = false;
@@ -248,6 +251,7 @@ Future<void> runBackgroundTask(
TimeLogger tlog, {
String mode = 'normal',
Duration? mlSelfStop,
+ Duration? mlLockWait,
}) async {
// Created at task start so a stop that fires before ML begins stays
// latched for the whole task.
@@ -271,7 +275,7 @@ Future<void> runBackgroundTask(
"[BG TASK] No recent foreground activity, proceeding with background work",
);
- await _runMinimally(taskId, tlog, mlRunControl);
+ await _runMinimally(taskId, tlog, mlRunControl, mlLockWait);
} finally {
mlSelfStopTimer.cancel();
}
@@ -281,6 +285,7 @@ Future<void> _runMinimally(
String taskId,
TimeLogger tlog,
MlRunControl mlRunControl,
+ Duration? mlLockWait,
) async {
try {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
@@ -375,6 +380,7 @@ Future<void> _runMinimally(
final disposition = await MLService.instance.runAllML(
force: false,
control: mlRunControl,
+ lockWait: mlLockWait,
);
_logger.info("[BG TASK] ML run disposition: ${disposition.name}");
} finally {
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
index b3178445ba..22a95a18e8 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -343,6 +343,7 @@ class MLService {
Future<MlRunDisposition> runAllML({
bool force = false,
MlRunControl? control,
+ Duration? lockWait,
}) async {
final runControl = control ?? MlRunControl();
// A latched stop always wins, force included.
@@ -386,6 +387,7 @@ class MLService {
control: runControl,
);
},
+ lockWait: lockWait,
);
if (attempt != MlLockAttempt.ran) {
_logger.info("runAllML denied the ml process lock (${attempt.name})");
@@ -495,18 +497,25 @@ class MLService {
Future<MlLockAttempt> _runExclusiveWithControl(
MlOperation operation,
MlRunControl control,
- Future<void> Function() body,
- ) async {
+ Future<void> Function() body, {
+ Duration? lockWait,
+ }) async {
_runControls.add(control);
try {
- return await MlProcessLock.instance.tryRunExclusive(operation, () async {
- _installRunControl(control);
- try {
- await body();
- } finally {
- _clearRunControl(control);
- }
- }, background: isProcessBg);
+ return await MlProcessLock.instance.tryRunExclusive(
+ operation,
+ () async {
+ _installRunControl(control);
+ try {
+ await body();
+ } finally {
+ _clearRunControl(control);
+ }
+ },
+ background: isProcessBg,
+ waitForAvailability: lockWait != null,
+ waitDeadline: lockWait,
+ );
} finally {
_runControls.remove(control);
}
diff --git a/mobile/apps/photos/lib/utils/bg_task_utils.dart b/mobile/apps/photos/lib/utils/bg_task_utils.dart
index 74ba025d47..5aa6bd2461 100644
--- a/mobile/apps/photos/lib/utils/bg_task_utils.dart
+++ b/mobile/apps/photos/lib/utils/bg_task_utils.dart
@@ -32,6 +32,7 @@ void callbackDispatcher() {
taskName,
tlog,
mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName),
+ mlLockWait: BgTaskUtils.mlLockWaitFor(taskName),
).timeout(
BgTaskUtils.taskTimeoutFor(taskName),
onTimeout: () async {
@@ -89,6 +90,11 @@ class BgTaskUtils {
: kBGTaskMLSelfStopIOS;
}
+ static Duration? mlLockWaitFor(String taskName) {
+ if (!Platform.isIOS || taskName != iOSBackgroundProcessingTask) return null;
+ return kBGProcessingTaskMLLockWaitIOS;
+ }
+
static const _kProcessingTaskStartTimeKey = "ios_processing_task_start_time";
static Future<void> markProcessingTaskStart(SharedPreferences prefs) async {