π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] Tidy ML lock acquisition and make hydration stoppable
laurenspriem5da9c19dae36
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.dartindex c2db471066..4bd8c9ea5b 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@@ -39,9 +39,9 @@ class MlProcessLock {final Lock _funnel = Lock();MlOperation? _activeOperation;- /// Whether an operation currently holds the funnel. For asserts and+ /// Whether the funnel is held or has queued waiters. For asserts and/// diagnostics only β never use this to skip [tryRunExclusive].- bool get isBusy => _activeOperation != null;+ bool get isBusy => _funnel.locked;MlOperation? get activeOperation => _activeOperation;diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dartindex 4ff30b3b82..ef1904b580 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart@@ -249,11 +249,14 @@ class MLService {);return existing;}- final attempt = await MlProcessLock.instance.tryRunExclusive(+ final control = MlRunControl();+ final attempt = await _runExclusiveWithControl(MlOperation.startupRemoteHydration,+ control,() async {final future = _runOwnedRemoteHydrationSafely(reason: reason,+ control: control,skipHydrationIfCandidateFileCountAtMost:skipHydrationIfCandidateFileCountAtMost,);@@ -266,7 +269,6 @@ class MLService {}}},- background: isProcessBg,);if (attempt != MlLockAttempt.ran) {_logger.info(@@ -278,12 +280,14 @@ class MLService {Future<void> _runOwnedRemoteHydrationSafely({required String reason,+ required MlRunControl control,int? skipHydrationIfCandidateFileCountAtMost,}) async {assert(MlProcessLock.instance.isBusy, "ml funnel must be held");try {await _hydrateRemoteEmbeddingsForOwnedFilesInternal(reason: reason,+ control: control,skipHydrationIfCandidateFileCountAtMost:skipHydrationIfCandidateFileCountAtMost,);@@ -294,10 +298,12 @@ class MLService {Future<void> _hydrateRemoteEmbeddingsForOwnedFilesInternal({required String reason,+ required MlRunControl control,int? skipHydrationIfCandidateFileCountAtMost,}) async {final summary = await hydrateOwnedRemoteMLData(mlDataDB: MLDataDB.instance,+ control: control,skipHydrationIfCandidateFileCountAtMost:skipHydrationIfCandidateFileCountAtMost,);@@ -388,8 +394,9 @@ class MLService {}var disposition = MlRunDisposition.completed;try {- final attempt = await MlProcessLock.instance.tryRunExclusive(+ final attempt = await _runExclusiveWithControl(MlOperation.fullRun,+ runControl,() async {disposition = await _runAllMLProtected(mode: mode,@@ -398,7 +405,6 @@ class MLService {control: runControl,);},- background: isProcessBg,);if (attempt != MlLockAttempt.ran) {_logger.info("runAllML denied the ml process lock (${attempt.name})");@@ -439,7 +445,6 @@ class MLService {required MlRunControl control,}) async {assert(MlProcessLock.instance.isBusy, "ml funnel must be held");- _installRunControl(control);try {await sync();if (control.stopRequested) {@@ -503,7 +508,6 @@ class MLService {return MlRunDisposition.completed;} finally {_logger.info("ML finished running");- _clearRunControl(control);if (!isProcessBg) {VideoPreviewService.instance.queueFiles();}@@ -515,6 +519,21 @@ class MLService {return MlRunDisposition.stopped;}+ Future<MlLockAttempt> _runExclusiveWithControl(+ MlOperation operation,+ MlRunControl control,+ Future<void> Function() body,+ ) {+ return MlProcessLock.instance.tryRunExclusive(operation, () async {+ _installRunControl(control);+ try {+ await body();+ } finally {+ _clearRunControl(control);+ }+ }, background: isProcessBg);+ }+void _installRunControl(MlRunControl control) {_activeRunControl = control;control.attachOnStop(() {@@ -563,17 +582,10 @@ class MLService {/// with the lastest faceMlVersion and stored on remote or local database. If so, it skips the image.Future<void> fetchAndIndexAllImages({required MLMode mode}) async {final control = MlRunControl();- final attempt = await MlProcessLock.instance.tryRunExclusive(+ final attempt = await _runExclusiveWithControl(MlOperation.indexing,- () async {- _installRunControl(control);- try {- await _fetchAndIndexAllImages(mode: mode, control: control);- } finally {- _clearRunControl(control);- }- },- background: isProcessBg,+ control,+ () => _fetchAndIndexAllImages(mode: mode, control: control),);if (attempt != MlLockAttempt.ran) {_logger.info(@@ -694,21 +706,14 @@ class MLService {bool force = false,}) async {final control = MlRunControl();- final attempt = await MlProcessLock.instance.tryRunExclusive(+ final attempt = await _runExclusiveWithControl(MlOperation.clustering,- () async {- _installRunControl(control);- try {- await _clusterAllImages(- clusterInBuckets: clusterInBuckets,- force: force,- control: control,- );- } finally {- _clearRunControl(control);- }- },- background: isProcessBg,+ control,+ () => _clusterAllImages(+ clusterInBuckets: clusterInBuckets,+ force: force,+ control: control,+ ),);if (attempt != MlLockAttempt.ran) {_logger.info(diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dartindex 2541228f54..39f08ff25a 100644--- a/mobile/apps/photos/lib/utils/ml_util.dart+++ b/mobile/apps/photos/lib/utils/ml_util.dart@@ -30,6 +30,7 @@ import "package:photos/services/machine_learning/face_ml/face_alignment/alignmenimport "package:photos/services/machine_learning/face_ml/face_detection/detection.dart";import "package:photos/services/machine_learning/ml_exceptions.dart";import "package:photos/services/machine_learning/ml_result.dart";+import "package:photos/services/machine_learning/ml_run_control.dart";import "package:photos/services/search_service.dart";import "package:photos/services/sync/origin_fetch_tracker.dart";import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;@@ -462,6 +463,7 @@ Stream<List<FileMLInstruction>> fetchEmbeddingsAndInstructions(Future<RemoteMLHydrationSummary> hydrateOwnedRemoteMLData({required MLDataDB mlDataDB,+ MlRunControl? control,int? skipHydrationIfCandidateFileCountAtMost,}) async {final candidateSplit = await _getOnlineFilesForMlIndexingCandidates();@@ -489,6 +491,9 @@ Future<RemoteMLHydrationSummary> hydrateOwnedRemoteMLData({start < ownedCandidates.length;start += embeddingFetchLimit) {+ if (control?.stopRequested ?? false) {+ break;+ }final end = math.min(start + embeddingFetchLimit, ownedCandidates.length);final chunk = ownedCandidates.sublist(start, end);final facePendingBefore = chunk.where((i) => i.shouldRunFaces).length;