Skip to content

armgit / ente

Public Imported from GitHub
0

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

[mobile][photos] Fix ML retry stalls and BG compute leak from review

laurenspriem21dad2ce3106
diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dart
index 8d9b267587..665e3b9bea 100644
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@ -255,10 +255,7 @@ Future<void> runBackgroundTask(
);
try {
- // Check if foreground is recently active to avoid conflicts
final isRunningInFG = await isForegroundEngineActive();
-
- // If FG was active in the last 5 seconds, skip BG work
if (isRunningInFG) {
_logger.info(
"[BG TASK] Foreground recently active, skipping background work",
@@ -375,20 +372,16 @@ Future<void> _runMinimally(
"[BG TASK] skipping ML, compute requirements not satisfied",
);
} else {
- bool mlRunStarted = false;
try {
await MLService.instance.init();
PersonService.init(entityService, MLDataDB.instance, prefs);
- mlRunStarted = true;
final disposition = await MLService.instance.runAllML(
force: false,
control: mlRunControl,
);
_logger.info("[BG TASK] ML run disposition: ${disposition.name}");
} finally {
- if (!mlRunStarted) {
- controller.releaseCompute(ml: true);
- }
+ controller.releaseCompute(ml: true);
}
}
}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart b/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart
index 7335fe1972..90006d42b9 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart
@@ -34,14 +34,12 @@ class MlProcessLock {
/// diagnostics only β€” never use this to skip [tryRunExclusive].
bool get isBusy => _activeOperation != null;
- /// The operation currently holding the funnel, if any. For derived status
- /// and diagnostics only.
MlOperation? get activeOperation => _activeOperation;
/// Runs [body] while holding the funnel and the native `ml` lock. The lock
- /// is released only after [body] completes, so [body] must drain any work
- /// it started before returning. Errors from [body] propagate after release.
- /// [background] labels this engine's origin for diagnostics.
+ /// is released only after [body] completes, so [body] must drain any
+ /// protected ML work it started before returning. Errors from [body]
+ /// propagate after release. [background] labels this engine's origin.
Future<MlLockAttempt> tryRunExclusive(
MlOperation operation,
Future<void> Function() body, {
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart b/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart
index c82c70e2c6..5d90a883b4 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart
@@ -19,7 +19,6 @@ class MlRunControl {
bool get stopRequested => _stopRequested;
MlStopReason? get stopReason => _stopReason;
- /// Idempotent; the first reason wins.
void requestStop(MlStopReason reason) {
if (_stopRequested) return;
_stopRequested = true;
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 53d7c531e2..4ff30b3b82 100644
--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart
@@ -366,6 +366,7 @@ class MLService {
}
if (_isRunningML) {
_logger.info("runAllML called while already running, skipping");
+ _scheduleDeniedRunRetry();
return MlRunDisposition.denied;
}
if (!hasGrantedMLConsent) {
@@ -423,6 +424,10 @@ class MLService {
_deniedRunRetryTimer = Timer(_kDeniedRunRetryDelay, () {
_deniedRunRetryTimer = null;
if (!hasGrantedMLConsent) return;
+ if (MlProcessLock.instance.isBusy) {
+ _scheduleDeniedRunRetry();
+ return;
+ }
triggerML();
});
}
@@ -461,8 +466,8 @@ class MLService {
_logger.info("App mode changed during ML run, stopping");
return MlRunDisposition.stopped;
}
- // Refresh discover/memories caches before indexing using the same
- // path in foreground and background runs.
+ // Cache refreshes only read ML stores, so they may safely outlive
+ // this run and the process lock.
magicCacheService.updateCache(forced: force).ignore();
memoriesCacheService.updateCache(forced: force).ignore();
}
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/ProcessLockChannelAdapter.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/ProcessLockChannelAdapter.swift
index 1207009ba3..460009daf8 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/ProcessLockChannelAdapter.swift
+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/Flutter/ProcessLockChannelAdapter.swift
@@ -26,7 +26,12 @@ final class ProcessLockChannelAdapter: NSObject {
}
private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
- guard isAttached else { return }
+ guard isAttached else {
+ result(
+ FlutterError(code: "detached", message: "adapter detached", details: nil)
+ )
+ return
+ }
switch call.method {
case "processLock.tryAcquire":
handleTryAcquire(call, result: result)
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/process_lock.dart b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/process_lock.dart
index 6dfbbda54c..85c2fa829f 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/process_lock.dart
+++ b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/process_lock.dart
@@ -1,6 +1,5 @@
import 'package:flutter/services.dart';
-/// Diagnostic state of a named process lock.
class ProcessLockState {
const ProcessLockState({
required this.origin,
@@ -64,8 +63,6 @@ class ProcessLockClient {
return result;
}
- /// Releases [name] if this engine holds it. Returns whether a release
- /// occurred.
Future<bool> release({required String name}) async {
final result = await _methodChannel.invokeMethod<bool>(
'processLock.release',
@@ -77,7 +74,6 @@ class ProcessLockClient {
return result;
}
- /// Returns the current holder of [name], or null when the lock is free.
Future<ProcessLockState?> state({required String name}) async {
final result = await _methodChannel.invokeMethod<Map<dynamic, dynamic>>(
'processLock.state',