π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] Fix ML stop acquisition race
laurenspriem835e991f52d5
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart b/mobile/apps/photos/lib/services/machine_learning/ml_service.dartindex 0630b8515b..f5ffe58f4b 100644--- a/mobile/apps/photos/lib/services/machine_learning/ml_service.dart+++ b/mobile/apps/photos/lib/services/machine_learning/ml_service.dart@@ -63,6 +63,7 @@ class MLService {bool debugIndexingDisabled = false;bool _clusteringIsHappening = false;bool _mlControllerStatus = false;+ final Set<MlRunControl> _runControls = {};MlRunControl? _activeRunControl;Timer? _bgYieldPollTimer;Timer? _deniedRunRetryTimer;@@ -280,6 +281,10 @@ class MLService {int? skipHydrationIfCandidateFileCountAtMost,}) async {assert(MlProcessLock.instance.isBusy, "ml funnel must be held");+ if (control.stopRequested) {+ _logRunStopped(control, "before owned remote hydration started");+ return;+ }try {final summary = await hydrateOwnedRemoteMLData(mlDataDB: MLDataDB.instance,@@ -425,6 +430,10 @@ class MLService {required MlRunControl control,}) async {assert(MlProcessLock.instance.isBusy, "ml funnel must be held");+ if (control.stopRequested) {+ _logRunStopped(control, "before sync");+ return MlRunDisposition.stopped;+ }final mlDataDB = _dbForMode(mode);try {await _sync();@@ -492,15 +501,20 @@ class MLService {MlOperation operation,MlRunControl control,Future<void> Function() body,- ) {- return MlProcessLock.instance.tryRunExclusive(operation, () async {- _installRunControl(control);- try {- await body();- } finally {- _clearRunControl(control);- }- }, background: isProcessBg);+ ) async {+ _runControls.add(control);+ try {+ return await MlProcessLock.instance.tryRunExclusive(operation, () async {+ _installRunControl(control);+ try {+ await body();+ } finally {+ _clearRunControl(control);+ }+ }, background: isProcessBg);+ } finally {+ _runControls.remove(control);+ }}void _installRunControl(MlRunControl control) {@@ -538,11 +552,13 @@ class MLService {}}- /// Requests a durable stop of the active ML run, if any. The run drains- /// already-started work and does not begin further stages; a new run may+ /// Requests a durable stop of all acquiring or active ML runs. Active runs+ /// drain already-started work and do not begin further stages; a new run may/// start later through the normal triggers.void stopActiveRun(MlStopReason reason) {- _activeRunControl?.requestStop(reason);+ for (final control in _runControls.toList(growable: false)) {+ control.requestStop(reason);+ }}/// Analyzes all the images in the user library with the latest ml version and stores the results in the database.@@ -569,11 +585,11 @@ class MLService {required MlRunControl control,}) async {assert(MlProcessLock.instance.isBusy, "ml funnel must be held");- if (!_canRunMLFunction(function: "Indexing")) return;if (control.stopRequested) {_logRunStopped(control, "before indexing started");return;}+ if (!_canRunMLFunction(function: "Indexing")) return;bool rustRuntimePrepared = false;try {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.swiftindex 460009daf8..8083262707 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@@ -46,9 +46,9 @@ final class ProcessLockChannelAdapter: NSObject {private func handleTryAcquire(_ call: FlutterMethodCall, result: FlutterResult) {guard let args = call.arguments as? [String: Any],- let name = args["name"] as? String, !name.isEmpty,- let origin = args["origin"] as? String, !origin.isEmpty,- let operation = args["operation"] as? String, !operation.isEmpty+ let name = args["name"] as? String, !name.isEmpty,+ let origin = args["origin"] as? String, !origin.isEmpty,+ let operation = args["operation"] as? String, !operation.isEmptyelse {result(FlutterError(@@ -71,7 +71,7 @@ final class ProcessLockChannelAdapter: NSObject {private func handleRelease(_ call: FlutterMethodCall, result: FlutterResult) {guard let args = call.arguments as? [String: Any],- let name = args["name"] as? String, !name.isEmpty+ let name = args["name"] as? String, !name.isEmptyelse {result(FlutterError(@@ -87,7 +87,7 @@ final class ProcessLockChannelAdapter: NSObject {private func handleState(_ call: FlutterMethodCall, result: FlutterResult) {guard let args = call.arguments as? [String: Any],- let name = args["name"] as? String, !name.isEmpty+ let name = args["name"] as? String, !name.isEmptyelse {result(FlutterError(diff --git a/mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/ProcessLockRegistry.swift b/mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/ProcessLockRegistry.swiftindex 8bd3d283c9..08105d02c7 100644--- a/mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/ProcessLockRegistry.swift+++ b/mobile/apps/photos/plugins/ente_photos_platform/ios/Sources/PhotosPlatformCore/ProcessLockRegistry.swift@@ -60,7 +60,8 @@ final class ProcessLockRegistry: @unchecked Sendable {defer { lock.unlock() }let released = holders.filter { $0.value.instanceId == instanceId }if !released.isEmpty {- let description = released+ let description =+ released.map { "\($0.key) (\($0.value.origin)/\($0.value.operation))" }.joined(separator: ", ")NSLog(