Skip to content

armgit / ente

Public Imported from GitHub
0

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

Merge remote-tracking branch 'upstream/main' into ios_background

laurenspriem5fcb6d0c497c
diff --cc mobile/apps/photos/lib/main.dart
index 9caafd3008,3fde99bae9..6db253462e
--- a/mobile/apps/photos/lib/main.dart
+++ b/mobile/apps/photos/lib/main.dart
@@@ -257,10 -259,18 +266,19 @@@ Future<void> runBackgroundTask
// latched for the whole task.
final mlRunControl = MlRunControl();
final mlSelfStopTimer = Timer(
- Platform.isIOS ? kBGTaskMLSelfStopIOS : kBGTaskMLSelfStopAndroid,
+ mlSelfStop ??
+ (Platform.isIOS ? kBGTaskMLSelfStopIOS : kBGTaskMLSelfStopAndroid),
() => mlRunControl.requestStop(MlStopReason.backgroundDeadline),
);
+ final mlForegroundWatchTimer = Timer.periodic(
+ const Duration(milliseconds: 500),
+ (_) async {
+ if (mlRunControl.stopRequested) return;
+ if (await isForegroundEngineActive()) {
+ mlRunControl.requestStop(MlStopReason.foregroundActive);
+ }
+ },
+ );
try {
final isRunningInFG = await isForegroundEngineActive();
@@@ -275,9 -285,10 +293,10 @@@
"[BG TASK] No recent foreground activity, proceeding with background work",
);
- await _runMinimally(taskId, tlog, mlRunControl);
+ await _runMinimally(taskId, tlog, mlRunControl, mlLockWait);
} finally {
mlSelfStopTimer.cancel();
+ mlForegroundWatchTimer.cancel();
}
}
diff --cc mobile/apps/photos/lib/utils/bg_task_utils.dart
index 9f5191ecf3,54bd8af748..e0db166596
--- a/mobile/apps/photos/lib/utils/bg_task_utils.dart
+++ b/mobile/apps/photos/lib/utils/bg_task_utils.dart
@@@ -19,51 -19,35 +20,59 @@@ void callbackDispatcher()
// Deferred error construction: an eagerly created Future.error with no
// listener surfaces as an unhandled exception even on success.
String? failure = "Task didn't run";
+ bool timedOut = false;
final prefs = await SharedPreferences.getInstance();
- await runWithLogs(() async {
- try {
- BgTaskUtils.$.info('Task started $tlog');
- if (Platform.isIOS &&
- taskName == BgTaskUtils.iOSBackgroundProcessingTask) {
- await BgTaskUtils.scheduleIOSBackgroundProcessingTask();
- await BgTaskUtils.markProcessingTaskStart(prefs);
+ await runWithLogs(
+ () async {
+ try {
+ BgTaskUtils.$.info('Task started $tlog');
++ if (Platform.isIOS &&
++ taskName == BgTaskUtils.iOSBackgroundProcessingTask) {
++ await BgTaskUtils.scheduleIOSBackgroundProcessingTask();
++ await BgTaskUtils.markProcessingTaskStart(prefs);
++ }
++ final Duration taskBudget = BgTaskUtils.taskTimeoutFor(taskName);
+ final Duration remainingBudget = Platform.isIOS
- ? kBGTaskTimeout - taskStopwatch.elapsed
- : const Duration(hours: 1);
- await runBackgroundTask(taskName, tlog).timeout(
++ ? taskBudget - taskStopwatch.elapsed
++ : taskBudget;
++ await runBackgroundTask(
++ taskName,
++ tlog,
++ mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName),
++ mlLockWait: BgTaskUtils.mlLockWaitFor(taskName),
++ ).timeout(
+ remainingBudget.isNegative ? Duration.zero : remainingBudget,
+ onTimeout: () async {
++ timedOut = true;
+ BgTaskUtils.$.warning(
+ "TLE, committing seppuku for taskID: $taskName",
+ );
+ await BgTaskUtils.releaseResourcesForKill(taskName, prefs);
+ },
+ );
- BgTaskUtils.$.info('Task run successful $tlog');
- failure = null;
++ if (timedOut) {
++ failure = "Task timed out";
++ } else {
++ BgTaskUtils.$.info('Task run successful $tlog');
++ failure = null;
++ }
+ } catch (e) {
+ BgTaskUtils.$.warning('Task error: $e');
+ await BgTaskUtils.releaseResourcesForKill(taskName, prefs);
+ failure = e.toString();
++ } finally {
++ // A timed-out run may still be draining, so its marker stays set.
++ if (!timedOut &&
++ Platform.isIOS &&
++ taskName == BgTaskUtils.iOSBackgroundProcessingTask) {
++ await BgTaskUtils.clearProcessingTaskStart(prefs);
++ }
}
- await runBackgroundTask(
- taskName,
- tlog,
- mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName),
- mlLockWait: BgTaskUtils.mlLockWaitFor(taskName),
- ).timeout(
- BgTaskUtils.taskTimeoutFor(taskName),
- onTimeout: () async {
- timedOut = true;
- BgTaskUtils.$.warning(
- "TLE, committing seppuku for taskID: $taskName",
- );
- await BgTaskUtils.releaseResourcesForKill(taskName, prefs);
- },
- );
- if (timedOut) {
- failure = "Task timed out";
- } else {
- BgTaskUtils.$.info('Task run successful $tlog');
- failure = null;
- }
- } catch (e) {
- BgTaskUtils.$.warning('Task error: $e');
- await BgTaskUtils.releaseResourcesForKill(taskName, prefs);
- failure = e.toString();
- } finally {
- // A timed-out run may still be draining, so its marker stays set.
- if (!timedOut &&
- Platform.isIOS &&
- taskName == BgTaskUtils.iOSBackgroundProcessingTask) {
- await BgTaskUtils.clearProcessingTaskStart(prefs);
- }
- }
- }, prefix: "[bg]").onError((_, _) {
+ },
+ prefix: "[bg]",
+ sentryInitTimeout: const Duration(seconds: 5),
+ ).onError((_, _) {
failure = "Didn't finished correctly!";
return;
});