[ios] iCloud synchronization bug when the file is moved to the Recently deleted #9872

Open
opened 2024-12-16 10:56:12 +00:00 by kirylkaveryn · 0 comments
Member

Bug description

There is a rare bug when during file editing the iCloud sync triggers the file deletion to the "recently deleted" list in the notification that the iCloud sends to the application.

Overview

Generally, iCloud's NSMetadataQuery sends a lot of events.
And during the running phase the update notification contains the lists of added/updated/deleted items:
https://developer.apple.com/documentation/foundation/nsmetadataquery/keys_for_use_with_a_notification_info_dictionary?language=objc

The files can be achieved by provided keys.

Logs

The logs from the user shows the bug process:

------- 1. The file was updated by the user:
I(7) 303.94055 map/bookmark_helpers.cpp:595 SaveKmlFileSafe(): Save kml file of type Text to /var/mobile/Containers/Data/Application/.../Documents/bookmarks/OrganicMaps.kml
I(1) 304.47096 LocalDirectoryMonitor.swift:170 didUpdate(:): Local contents (2):
I(1) 304.47110 LocalDirectoryMonitor.swift:171 didUpdate(
:): fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 304.47116 LocalDirectoryMonitor.swift:171 didUpdate(:): fileName: Мои Метки.kml, lastModified: 1667487633.0
I(1) 304.47124 LocalDirectoryMonitor.swift:172 didUpdate(
:): Added to the local content (0):
I(1) 304.47130 LocalDirectoryMonitor.swift:173 didUpdate(:): Updated in the local content (1): <--------------------- 2. We get the update from of the local directory - the user changes the file (add/delete bookmark)
fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 304.47137 LocalDirectoryMonitor.swift:174 didUpdate(
:): Removed from the local content (0):
I(1) 304.47146 SynchronizationStateResolver.swift:74 resolveEvent(:): Events to process (1):
I(1) 304.47184 SynchronizationStateResolver.swift:75 resolveEvent(
:): updateCloudItem(with: Organic_Maps.LocalMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///var/mobile/Containers/Data/Application/.../Documents/bookmarks/OrganicMaps.kml, lastModificationDate: 1732783499.0)) <--------------------- 3. Event to update the cloud version of the file
I(9) 304.47208 SynchronizationFileWriter.swift:110 writeToCloudContainer(:completion:): Write file OrganicMaps.kml to the cloud directory
I(1) 304.55711 CloudDirectoryMonitor.swift:182 queryDidUpdate(
:): Cloud contents (1): <--------------------- 4. For some unknown reason the file "OrganicMaps.kml" was removed from the cloud and placed to the "Removed" list
I(1) 304.55718 CloudDirectoryMonitor.swift:183 queryDidUpdate(:): fileName: Мои Метки.kml, lastModified: 1667487633.0
I(1) 304.55721 CloudDirectoryMonitor.swift:184 queryDidUpdate(
:): Added to the cloud content (0):
I(1) 304.55723 CloudDirectoryMonitor.swift:185 queryDidUpdate(:): Updated in the cloud content (0):
I(1) 304.55725 CloudDirectoryMonitor.swift:186 queryDidUpdate(
:): Removed from the cloud content (1): <--------------------- 5. I don't know why the "OrganicMaps.kml" is placed to the "Removed" list...
fileName: OrganicMaps.kml, lastModified: 1732783496.0
I(1) 304.55729 SynchronizationStateResolver.swift:74 resolveEvent(:): Events to process (1):
I(1) 304.55750 SynchronizationStateResolver.swift:75 resolveEvent(
:): removeLocalItem(Organic_Maps.LocalMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///var/mobile/Containers/Data/Application/.../Documents/bookmarks/OrganicMaps.kml, lastModificationDate: 1732783499.0)) <--------------------- 6. This triggers the file deletion to the Recently deleted
I(10) 304.55770 SynchronizationFileWriter.swift:85 removeFromLocalContainer(:completion:): Remove file OrganicMaps.kml from the local directory
I(1) 304.56199 map/bookmark_manager.cpp:2625 DeleteBmCategory(): Category at /var/mobile/Containers/Data/Application.../Documents/bookmarks/OrganicMaps.kml is trashed to the /var/mobile/Containers/Data/Application.../Documents/.Trash/OrganicMaps.kml
I(1) 305.06629 CloudDirectoryMonitor.swift:182 queryDidUpdate(
:): Cloud contents (2):
I(1) 305.06637 CloudDirectoryMonitor.swift:183 queryDidUpdate(:): fileName: Мои Метки.kml, lastModified: 1667487633.0
I(1) 305.06640 CloudDirectoryMonitor.swift:183 queryDidUpdate(
:): fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 305.06643 CloudDirectoryMonitor.swift:184 queryDidUpdate(:): Added to the cloud content (1): <--------------------- 7. After a few time the iCloud sends ONE MORE notification where the file is Present in the list and marked ad Added (so it was firstly removed and and than added). This issue creates an undefined behaviour with an attemp to recreate a file.
fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 305.06646 CloudDirectoryMonitor.swift:185 queryDidUpdate(
:): Updated in the cloud content (0):
I(1) 305.06649 CloudDirectoryMonitor.swift:186 queryDidUpdate(:): Removed from the cloud content (0):
I(1) 305.06653 SynchronizationStateResolver.swift:74 resolveEvent(
:): Events to process (0):
I(1) 305.06856 LocalDirectoryMonitor.swift:170 didUpdate(:): Local contents (1):
I(1) 305.06860 LocalDirectoryMonitor.swift:171 didUpdate(
:): fileName: Мои Метки.kml, lastModified: 1667487633.0
I(1) 305.06863 LocalDirectoryMonitor.swift:172 didUpdate(:): Added to the local content (0):
I(1) 305.06865 LocalDirectoryMonitor.swift:173 didUpdate(
:): Updated in the local content (0):
I(1) 305.06867 LocalDirectoryMonitor.swift:174 didUpdate(:): Removed from the local content (1):
fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 305.06871 SynchronizationStateResolver.swift:74 resolveEvent(
:): Events to process (1):
I(1) 305.06900 SynchronizationStateResolver.swift:75 resolveEvent(:): removeCloudItem(Organic_Maps.CloudMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///var/mobile/Library/Mobile%20Documents/iCloudapporganicmaps/Documents/OrganicMaps.kml, isDownloaded: true, lastModificationDate: 1732783499.0, downloadingError: nil, uploadingError: nil, hasUnresolvedConflicts: false))
I(8) 305.06913 SynchronizationFileWriter.swift:129 removeFromCloudContainer(
:completion:): Trash file OrganicMaps.kml to the iCloud trash
I(1) 305.40873 CloudDirectoryMonitor.swift:182 queryDidUpdate(:): Cloud contents (2):
I(1) 305.40877 CloudDirectoryMonitor.swift:183 queryDidUpdate(
:): fileName: Мои Метки.kml, lastModified: 1667487633.0
I(1) 305.40880 CloudDirectoryMonitor.swift:183 queryDidUpdate(:): fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 305.40881 CloudDirectoryMonitor.swift:184 queryDidUpdate(
:): Added to the cloud content (0):
I(1) 305.40883 CloudDirectoryMonitor.swift:185 queryDidUpdate(:): Updated in the cloud content (1): <--------------------- 8. And after all the icloud sends the Update event for the file that was previously moves to the icloud trash... this cause and error and stops a sync
fileName: OrganicMaps.kml, lastModified: 1732783499.0
I(1) 305.40884 CloudDirectoryMonitor.swift:186 queryDidUpdate(
:): Removed from the cloud content (0):
I(1) 305.40886 SynchronizationStateResolver.swift:74 resolveEvent(:): Events to process (1):
I(1) 305.40899 SynchronizationStateResolver.swift:75 resolveEvent(
:): createLocalItem(with: Organic_Maps.CloudMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///private/var/mobile/Library/Mobile%20Documents/iCloudapporganicmaps/Documents/OrganicMaps.kml, isDownloaded: true, lastModificationDate: 1732783499.0, downloadingError: nil, uploadingError: nil, hasUnresolvedConflicts: false))
I(10) 305.41030 SynchronizationFileWriter.swift:66 writeToLocalContainer(:completion:): Write file OrganicMaps.kml to the local directory
E(1) 305.42882 SynchronizaionManager.swift:286 processError(
:): System Error: Не удалось открыть файл «OrganicMaps.kml», так как такого файла не существует.
I(1) 305.42884 SynchronizaionManager.swift:139 stopSynchronization(withError:): Stop synchronization
I(1) 305.42927 SynchronizaionManager.swift:139 stopSynchronization(withError:): Stop synchronization

I cant find the answer ont the question why the file updating triggers the file deletion and then creation. Because the updating code is made in the safe-replace manner:

87526aad54/iphone/Maps/Core/iCloud/SynchronizationFileWriter.swift (L109-L117)

87526aad54/iphone/Maps/Core/iCloud/SynchronizationFileWriter.swift (L242-L255)

### Bug description There is a rare bug when during file editing the iCloud sync triggers the file deletion to the "recently deleted" list in the notification that the iCloud sends to the application. ### Overview Generally, iCloud's [NSMetadataQuery](https://developer.apple.com/documentation/foundation/nsmetadataquery?language=objc) sends a lot of events. And during the running phase the [update notification](NSMetadataQueryDidUpdateNotification) contains the lists of added/updated/deleted items: https://developer.apple.com/documentation/foundation/nsmetadataquery/keys_for_use_with_a_notification_info_dictionary?language=objc The files can be achieved by provided keys. ### Logs The logs from the user shows the bug process: > ------- **1. The file was updated by the user:** I(7) 303.94055 map/bookmark_helpers.cpp:595 SaveKmlFileSafe(): Save kml file of type Text to /var/mobile/Containers/Data/Application/.../Documents/bookmarks/OrganicMaps.kml I(1) 304.47096 LocalDirectoryMonitor.swift:170 didUpdate(_:): Local contents (2): I(1) 304.47110 LocalDirectoryMonitor.swift:171 didUpdate(_:): fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 304.47116 LocalDirectoryMonitor.swift:171 didUpdate(_:): fileName: Мои Метки.kml, lastModified: 1667487633.0 I(1) 304.47124 LocalDirectoryMonitor.swift:172 didUpdate(_:): Added to the local content (0): I(1) 304.47130 LocalDirectoryMonitor.swift:173 didUpdate(_:): Updated in the local content (1): **<--------------------- 2. We get the update from of the local directory - the user changes the file (add/delete bookmark)** fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 304.47137 LocalDirectoryMonitor.swift:174 didUpdate(_:): Removed from the local content (0): I(1) 304.47146 SynchronizationStateResolver.swift:74 resolveEvent(_:): Events to process (1): I(1) 304.47184 SynchronizationStateResolver.swift:75 resolveEvent(_:): updateCloudItem(with: Organic_Maps.LocalMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///var/mobile/Containers/Data/Application/.../Documents/bookmarks/OrganicMaps.kml, lastModificationDate: 1732783499.0)) **<--------------------- 3. Event to update the cloud version of the file** I(9) 304.47208 SynchronizationFileWriter.swift:110 writeToCloudContainer(_:completion:): Write file OrganicMaps.kml to the cloud directory I(1) 304.55711 CloudDirectoryMonitor.swift:182 queryDidUpdate(_:): Cloud contents (1): **<--------------------- 4. For some unknown reason the file "OrganicMaps.kml" was removed from the cloud and placed to the "Removed" list** I(1) 304.55718 CloudDirectoryMonitor.swift:183 queryDidUpdate(_:): fileName: Мои Метки.kml, lastModified: 1667487633.0 I(1) 304.55721 CloudDirectoryMonitor.swift:184 queryDidUpdate(_:): Added to the cloud content (0): I(1) 304.55723 CloudDirectoryMonitor.swift:185 queryDidUpdate(_:): Updated in the cloud content (0): I(1) 304.55725 CloudDirectoryMonitor.swift:186 queryDidUpdate(_:): Removed from the cloud content (1): **<--------------------- 5. I don't know why the "OrganicMaps.kml" is placed to the "Removed" list...** fileName: OrganicMaps.kml, lastModified: 1732783496.0 I(1) 304.55729 SynchronizationStateResolver.swift:74 resolveEvent(_:): Events to process (1): I(1) 304.55750 SynchronizationStateResolver.swift:75 resolveEvent(_:): removeLocalItem(Organic_Maps.LocalMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///var/mobile/Containers/Data/Application/.../Documents/bookmarks/OrganicMaps.kml, lastModificationDate: 1732783499.0)) **<--------------------- 6. This triggers the file deletion to the Recently deleted** I(10) 304.55770 SynchronizationFileWriter.swift:85 removeFromLocalContainer(_:completion:): Remove file OrganicMaps.kml from the local directory I(1) 304.56199 map/bookmark_manager.cpp:2625 DeleteBmCategory(): Category at /var/mobile/Containers/Data/Application.../Documents/bookmarks/OrganicMaps.kml is trashed to the /var/mobile/Containers/Data/Application.../Documents/.Trash/OrganicMaps.kml I(1) 305.06629 CloudDirectoryMonitor.swift:182 queryDidUpdate(_:): Cloud contents (2): I(1) 305.06637 CloudDirectoryMonitor.swift:183 queryDidUpdate(_:): fileName: Мои Метки.kml, lastModified: 1667487633.0 I(1) 305.06640 CloudDirectoryMonitor.swift:183 queryDidUpdate(_:): fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 305.06643 CloudDirectoryMonitor.swift:184 queryDidUpdate(_:): Added to the cloud content (1): **<--------------------- 7. After a few time the iCloud sends ONE MORE notification where the file is Present in the list and marked ad Added (so it was firstly removed and and than added). This issue creates an undefined behaviour with an attemp to recreate a file.** fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 305.06646 CloudDirectoryMonitor.swift:185 queryDidUpdate(_:): Updated in the cloud content (0): I(1) 305.06649 CloudDirectoryMonitor.swift:186 queryDidUpdate(_:): Removed from the cloud content (0): I(1) 305.06653 SynchronizationStateResolver.swift:74 resolveEvent(_:): Events to process (0): I(1) 305.06856 LocalDirectoryMonitor.swift:170 didUpdate(_:): Local contents (1): I(1) 305.06860 LocalDirectoryMonitor.swift:171 didUpdate(_:): fileName: Мои Метки.kml, lastModified: 1667487633.0 I(1) 305.06863 LocalDirectoryMonitor.swift:172 didUpdate(_:): Added to the local content (0): I(1) 305.06865 LocalDirectoryMonitor.swift:173 didUpdate(_:): Updated in the local content (0): I(1) 305.06867 LocalDirectoryMonitor.swift:174 didUpdate(_:): Removed from the local content (1): fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 305.06871 SynchronizationStateResolver.swift:74 resolveEvent(_:): Events to process (1): I(1) 305.06900 SynchronizationStateResolver.swift:75 resolveEvent(_:): removeCloudItem(Organic_Maps.CloudMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///var/mobile/Library/Mobile%20Documents/iCloud~app~organicmaps/Documents/OrganicMaps.kml, isDownloaded: true, lastModificationDate: 1732783499.0, downloadingError: nil, uploadingError: nil, hasUnresolvedConflicts: false)) I(8) 305.06913 SynchronizationFileWriter.swift:129 removeFromCloudContainer(_:completion:): Trash file OrganicMaps.kml to the iCloud trash I(1) 305.40873 CloudDirectoryMonitor.swift:182 queryDidUpdate(_:): Cloud contents (2): I(1) 305.40877 CloudDirectoryMonitor.swift:183 queryDidUpdate(_:): fileName: Мои Метки.kml, lastModified: 1667487633.0 I(1) 305.40880 CloudDirectoryMonitor.swift:183 queryDidUpdate(_:): fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 305.40881 CloudDirectoryMonitor.swift:184 queryDidUpdate(_:): Added to the cloud content (0): I(1) 305.40883 CloudDirectoryMonitor.swift:185 queryDidUpdate(_:): Updated in the cloud content (1): **<--------------------- 8. And after all the icloud sends the Update event for the file that was previously moves to the icloud trash... this cause and error and stops a sync** fileName: OrganicMaps.kml, lastModified: 1732783499.0 I(1) 305.40884 CloudDirectoryMonitor.swift:186 queryDidUpdate(_:): Removed from the cloud content (0): I(1) 305.40886 SynchronizationStateResolver.swift:74 resolveEvent(_:): Events to process (1): I(1) 305.40899 SynchronizationStateResolver.swift:75 resolveEvent(_:): createLocalItem(with: Organic_Maps.CloudMetadataItem(fileName: "OrganicMaps.kml", fileUrl: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~app~organicmaps/Documents/OrganicMaps.kml, isDownloaded: true, lastModificationDate: 1732783499.0, downloadingError: nil, uploadingError: nil, hasUnresolvedConflicts: false)) I(10) 305.41030 SynchronizationFileWriter.swift:66 writeToLocalContainer(_:completion:): Write file OrganicMaps.kml to the local directory E(1) 305.42882 SynchronizaionManager.swift:286 processError(_:): System Error: Не удалось открыть файл «OrganicMaps.kml», так как такого файла не существует. I(1) 305.42884 SynchronizaionManager.swift:139 stopSynchronization(withError:): Stop synchronization I(1) 305.42927 SynchronizaionManager.swift:139 stopSynchronization(withError:): Stop synchronization I cant find the answer ont the question why the _file updating_ triggers the file _deletion_ and then _creation_. Because the updating code is made in the [safe-replace manner](https://developer.apple.com/documentation/foundation/nsfilemanager/1412432-replaceitematurl?language=objc): https://github.com/organicmaps/organicmaps/blob/87526aad543f141f48600664ae12bb93ea0ce8b5/iphone/Maps/Core/iCloud/SynchronizationFileWriter.swift#L109-L117 https://github.com/organicmaps/organicmaps/blob/87526aad543f141f48600664ae12bb93ea0ce8b5/iphone/Maps/Core/iCloud/SynchronizationFileWriter.swift#L242-L255
This repo is archived. You cannot comment on issues.
No labels
Accessibility
Accessibility
Address
Address
Android
Android
Android Auto
Android Auto
Android Automotive (AAOS)
Android Automotive (AAOS)
API
API
AppGallery
AppGallery
AppStore
AppStore
Battery and Performance
Battery and Performance
Blocker
Blocker
Bookmarks and Tracks
Bookmarks and Tracks
Borders
Borders
Bug
Bug
Build
Build
CarPlay
CarPlay
Classificator
Classificator
Community
Community
Core
Core
CrashReports
CrashReports
Cycling
Cycling
Desktop
Desktop
DevEx
DevEx
DevOps
DevOps
dev_sandbox
dev_sandbox
Directions
Directions
Documentation
Documentation
Downloader
Downloader
Drape
Drape
Driving
Driving
Duplicate
Duplicate
Editor
Editor
Elevation
Elevation
Enhancement
Enhancement
Epic
Epic
External Map Datasets
External Map Datasets
F-Droid
F-Droid
Fonts
Fonts
Frequently User Reported
Frequently User Reported
Fund
Fund
Generator
Generator
Good first issue
Good first issue
Google Play
Google Play
GPS
GPS
GSoC
GSoC
iCloud
iCloud
Icons
Icons
iOS
iOS
Legal
Legal
Linux Desktop
Linux Desktop
Linux packaging
Linux packaging
Linux Phone
Linux Phone
Mac OS
Mac OS
Map Data
Map Data
Metro
Metro
Navigation
Navigation
Need Feedback
Need Feedback
Night Mode
Night Mode
NLnet 2024-06-281
NLnet 2024-06-281
No Feature Parity
No Feature Parity
Opening Hours
Opening Hours
Outdoors
Outdoors
POI Info
POI Info
Privacy
Privacy
Public Transport
Public Transport
Raw Idea
Raw Idea
Refactoring
Refactoring
Regional
Regional
Regression
Regression
Releases
Releases
RoboTest
RoboTest
Route Planning
Route Planning
Routing
Routing
Ruler
Ruler
Search
Search
Security
Security
Styles
Styles
Tests
Tests
Track Recording
Track Recording
Translations
Translations
TTS
TTS
UI
UI
UX
UX
Walk Navigation
Walk Navigation
Watches
Watches
Web
Web
Wikipedia
Wikipedia
Windows
Windows
Won't fix
Won't fix
World Map
World Map
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: organicmaps/organicmaps-tmp#9872
No description provided.