-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[mob][photos] Add iOS upload grace window and bg handoff #9810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prateekmedia
wants to merge
14
commits into
prtk/bg-patches
Choose a base branch
from
prtk/bg-grace-window
base: prtk/bg-patches
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c71eea4
Add iOS upload grace window and BGProcessing handoff
prateekmedia 6f163ca
Flatten upload grace window coordinator
prateekmedia 80788ec
Merge remote-tracking branch 'upstream/main' into prtk/bg-grace-window
prateekmedia d7cd73a
Merge remote-tracking branch 'upstream/main' into prtk/bg-grace-window
prateekmedia 4bec369
Merge branch 'prtk/bg-patches' into prtk/bg-grace-window
prateekmedia ec7ed51
Fix grace branch analyzer nits
prateekmedia 177b70a
Merge branch 'prtk/bg-patches' into prtk/bg-grace-window
prateekmedia e45da7d
Fix grace window expiration ownership
prateekmedia 6c6787d
Clear stale grace window marker on restart
prateekmedia d60d117
Avoid removing active upload queue entries
prateekmedia 1c46d45
Schedule grace continuation at start
prateekmedia e2682f4
Merge branch 'prtk/bg-patches' into prtk/bg-grace-window
prateekmedia ce0eb76
Merge branch 'prtk/bg-patches' into prtk/bg-grace-window
prateekmedia f7447d3
Merge branch 'prtk/bg-patches' into prtk/bg-grace-window
prateekmedia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
mobile/apps/photos/lib/services/upload_background_coordinator.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import "dart:async"; | ||
| import "dart:io"; | ||
|
|
||
| import "package:grace_window_ios/grace_window_ios.dart"; | ||
| import "package:logging/logging.dart"; | ||
| import "package:photos/db/upload_locks_db.dart"; | ||
| import "package:photos/service_locator.dart"; | ||
| import "package:photos/utils/bg_task_utils.dart"; | ||
| import "package:photos/utils/file_uploader.dart"; | ||
| import "package:shared_preferences/shared_preferences.dart"; | ||
|
|
||
| const _keyIOSUploadGraceActive = "ios_bg_upload_grace_active"; | ||
|
|
||
| final _logger = Logger("UploadBackgroundCoordinator"); | ||
| bool _isGraceWindowActiveInProcess = false; | ||
|
|
||
| Future<void> onUploadAppBackground() async { | ||
| if (!Platform.isIOS || !flagService.enableIOSBackgroundHandoff) { | ||
| return; | ||
| } | ||
|
|
||
| if (FileUploader.instance.hasActiveUploads) { | ||
| if (!flagService.enableIOSBackgroundGraceWindow) { | ||
| return; | ||
| } | ||
|
|
||
| final prefs = await SharedPreferences.getInstance(); | ||
| if (prefs.getBool(_keyIOSUploadGraceActive) ?? false) { | ||
| if (_isGraceWindowActiveInProcess) { | ||
| return; | ||
| } | ||
|
|
||
| _logger.info("Clearing stale iOS upload grace window marker"); | ||
| await prefs.remove(_keyIOSUploadGraceActive); | ||
| } | ||
|
|
||
| _logger.info("Starting iOS upload grace window"); | ||
| await GraceWindowIos.beginGraceWindow("ente-upload-grace-window"); | ||
| await prefs.setBool(_keyIOSUploadGraceActive, true); | ||
| _isGraceWindowActiveInProcess = true; | ||
| await BgTaskUtils.scheduleIOSBackgroundProcessingTask( | ||
| source: "graceWindowStarted", | ||
| initialDelay: BgTaskUtils.continuationDelay(), | ||
| reason: BgTaskUtils.iOSBackgroundProcessingReasonContinuation, | ||
| ); | ||
| unawaited(_waitForGraceWindowExpiration()); | ||
| return; | ||
| } | ||
|
|
||
| if (await BgTaskUtils.isIOSBackupEligible()) { | ||
| await BgTaskUtils.scheduleIOSBackgroundProcessingTask( | ||
| source: "appBackground:maintenance", | ||
| initialDelay: BgTaskUtils.maintenanceDelay(), | ||
| reason: BgTaskUtils.iOSBackgroundProcessingReasonMaintenance, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Future<void> onUploadAppForeground() async { | ||
| if (!Platform.isIOS || !flagService.enableIOSBackgroundHandoff) { | ||
| return; | ||
| } | ||
|
|
||
| if (!flagService.enableIOSBackgroundGraceWindow) { | ||
| await BgTaskUtils.cancelIOSBackgroundProcessingTask( | ||
| source: "appForeground", | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| final prefs = await SharedPreferences.getInstance(); | ||
| final graceWasActive = prefs.getBool(_keyIOSUploadGraceActive) ?? false; | ||
|
|
||
| if (graceWasActive) { | ||
| final cutoffMicros = DateTime.now().microsecondsSinceEpoch; | ||
|
|
||
| _logger.info("Finishing iOS upload grace window and reconciling uploads"); | ||
| await GraceWindowIos.endGraceWindow(); | ||
| final didExpireGraceWindow = await GraceWindowIos.consumeExpiredState(); | ||
| await prefs.remove(_keyIOSUploadGraceActive); | ||
| _isGraceWindowActiveInProcess = false; | ||
| if (didExpireGraceWindow) { | ||
| _logger.info( | ||
| "Grace window expiration was observed natively before foreground recovery", | ||
| ); | ||
| await UploadLocksDB.instance.releaseLocksAcquiredByOwnerBefore( | ||
| ProcessType.foreground.toString(), | ||
| cutoffMicros, | ||
| ); | ||
| } | ||
| await FileUploader.instance.reconcileAfterBackground(); | ||
| } | ||
|
|
||
| await BgTaskUtils.cancelIOSBackgroundProcessingTask( | ||
| source: "appForeground", | ||
| ); | ||
| } | ||
|
|
||
| /// Best-effort same-process expiration detection via a pending MethodChannel | ||
| /// call. Swift holds the result and completes it when the native expiration | ||
| /// handler fires. If this path misses (e.g. process suspended before | ||
| /// delivery), [onUploadAppForeground] catches it via [consumeExpiredState]. | ||
| Future<void> _waitForGraceWindowExpiration() async { | ||
| final expired = await GraceWindowIos.awaitExpiration(); | ||
| if (!expired) { | ||
| return; | ||
| } | ||
|
|
||
| final cutoffMicros = DateTime.now().microsecondsSinceEpoch; | ||
|
|
||
| _logger.info("iOS upload grace window expired (via pending call)"); | ||
|
|
||
| // Consume the durable marker *before* releasing locks so that | ||
| // onUploadAppForeground (which can run between any two awaits here) won't | ||
| // see the marker and do a second lock release against freshly | ||
| // reacquired locks. | ||
| final didOwnExpiration = await GraceWindowIos.consumeExpiredState(); | ||
| if (!didOwnExpiration) { | ||
| return; | ||
| } | ||
|
|
||
| await UploadLocksDB.instance.releaseLocksAcquiredByOwnerBefore( | ||
| ProcessType.foreground.toString(), | ||
| cutoffMicros, | ||
| ); | ||
|
|
||
| final prefs = await SharedPreferences.getInstance(); | ||
| await prefs.remove(_keyIOSUploadGraceActive); | ||
| _isGraceWindowActiveInProcess = false; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This flow relies on
awaitExpiration()resolving to enqueue the BGProcessing continuation task, but the comment already notes delivery can be missed when the process is suspended. In that case this await never resumes, soscheduleIOSBackgroundProcessingTask(...)is never called and active uploads can remain stranded in background until the app is foregrounded again (or a later periodic trigger happens). Please add a fallback path that does not depend solely on this pending MethodChannel reply (for example, durable scheduling on grace-window start or native-side expiry handling).Useful? React with 👍 / 👎.