NSPersistentCloudkitContainer Memory Leak -> Crash? (iOS 15 beta 4 & 5)

Background

I have an established app in the App Store which has been using NSPersistentCloudkitContainer since iOS 13 without any issues.

I've been running my app normally on an iOS device running the iOS 15 betas, mainly to see problems arise before my users see them.

Ever since iOS 15 (beta 4) my app has failed to sync changes - no matter how small the change. An upload 'starts' but never completes. After a minute or so the app quits to the Home Screen and no useful information can be gleaned from crash reports. Until now I've had no idea what's going on.

Possible Bug in the API?

I've managed to replicate this behaviour on the simulator and on another device when building my app with Xcode 13 (beta 5) on iOS 15 (beta 5).

It appears that NSPersistentCloudkitContainer has a memory leak and keeps ramping up the RAM consumption (and CPU at 100%) until the operating system kills the app. No code of mine is running.

I'm not really an expert on these things and I tried to use Instruments to see if that would show me anything. It appears to be related to NSCloudkitMirroringDelegate getting 'stuck' somehow but I have no idea what to do with this information.

My Core Data database is not tiny, but not massive by any means and NSPersistentCloudkitContainer has had no problems syncing to iCloud prior to iOS 15 (beta 4).

If I restore my App Data (from an external backup file - 700MB with lots of many-many, many-one relationships, ckAssets, etc.) the data all gets added to Core Data without an issue at all. The console log (see below) then shows that a sync is created, scheduled & then started... but no data is uploaded.

At this point the memory consumption starts and all I see is 'backgroundTask' warnings appear (only related to CloudKit) with no code of mine running.

CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExporter analyzeHistoryInStore:withManagedObjectContext:error:](501): <PFCloudKitExporter: 0x600000301450>: Exporting changes since (0): <NSPersistentHistoryToken - {
    "4B90A437-3D96-4AC9-A27A-E0F633CE5D9D" = 906;
}>

CoreData: CloudKit: CoreData+CloudKit: -[PFCloudKitExportContext processAnalyzedHistoryInStore:inManagedObjectContext:error:]_block_invoke_3(251): Finished processing analyzed history with 29501 metadata objects to create, 0 deleted rows without metadata.

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _scheduleAutomatedExportWithLabel:activity:completionHandler:](2800): <NSCloudKitMirroringDelegate: 0x6000015515c0> - Beginning automated export - ExportActivity:
<CKSchedulerActivity: 0x60000032c500; containerID=<CKContainerID: 0x600002ed3240; containerIdentifier=iCloud.com.nitramluap.Somnus, containerEnvironment="Sandbox">, identifier=com.apple.coredata.cloudkit.activity.export.4B90A437-3D96-4AC9-A27A-E0F633CE5D9D, priority=2, xpcActivityCriteriaOverrides={ Priority=Utility }>

CoreData: CloudKit: CoreData+CloudKit: -[NSCloudKitMirroringDelegate executeMirroringRequest:error:](765): <NSCloudKitMirroringDelegate: 0x6000015515c0>: Asked to execute request: <NSCloudKitMirroringExportRequest: 0x600002ed2a30> CBE1852D-7793-46B6-8314-A681D2038B38

2021-08-13 08:41:01.518422+1000 Somnus[11058:671570] [BackgroundTask] Background Task 68 ("CoreData: CloudKit Export"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

2021-08-13 08:41:03.519455+1000 Somnus[11058:671570] [BackgroundTask] Background Task 154 ("CoreData: CloudKit Scheduling"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

Just wondering if anyone else is having a similar issue? It never had a problem syncing an initial database restore prior to iOS 15 (beta 4) and the problems started right after installing iOS 15 (beta 4).

I've submitted this to Apple Feedback and am awaiting a response (FB9412346). If this is unfixable I'm in real trouble (and my users are going to be livid).

Thanks in advance!

Answered by nitramluap in 694468022

Quick Update

(Note: I've not received any official update from Apple yet)

I've just installed iOS 15.2 (beta 2 19C5036e) today and have noticed that data now seems to be uploading from my App (App Store version) when previously it would just sit there... then crash. If I look under iCloud -> Manage Storage -> MyApp I can see the figure for data stored going up slowly.

Better yet, I'm seeing data come DOWN to my other devices now.

I'm not near my development setup so I can't test this on a physical device to see what the console output shows, but I'll try and do this tonight on my iPad. I'll ingest my large external backup file into Core Data and see if it uploads to iCloud and report back.

There is no new update for Xcode yet so I don't think we can test with iOS 15.2 (beta 2) in a simulator environment. I'm not going to count my chickens just yet...

The problem may even result in fully crashed CoreData stack with data loss (iOS 15.1).

I submitted a case (FB9744735) including sysdiagnose and crash logs. From my point of view an absolutely critical bug.

Accepted Answer

Quick Update

(Note: I've not received any official update from Apple yet)

I've just installed iOS 15.2 (beta 2 19C5036e) today and have noticed that data now seems to be uploading from my App (App Store version) when previously it would just sit there... then crash. If I look under iCloud -> Manage Storage -> MyApp I can see the figure for data stored going up slowly.

Better yet, I'm seeing data come DOWN to my other devices now.

I'm not near my development setup so I can't test this on a physical device to see what the console output shows, but I'll try and do this tonight on my iPad. I'll ingest my large external backup file into Core Data and see if it uploads to iCloud and report back.

There is no new update for Xcode yet so I don't think we can test with iOS 15.2 (beta 2) in a simulator environment. I'm not going to count my chickens just yet...

Seem to finally sync correctly on iOS 15.2.
But I am seeing a really annoying rate limiting in development environment, is this rate limiting also applying in production environment ? Because if that's the case initial sync will take an enormous amount of time considering how much I am storing in core data and a rate limiting of 70-80 seconds...

To date with the official release of iOS 15 & macOS 12 using Xcode 13.2.1 this is still an issue. The problem is still reflected with the mirror delegate as the source of the leaks on the macOS where the app is terminated during a cloud sync when it goes over 120 Gigs of ram and on the phone.

broken again in ios16b1. doesn't crash but same error message and doesn't sync

test in iOS 16.4.1, This BUG still exists... ;(

This issue might be back when using SwiftData and CloudKit. On iOS 17.5.1 I'm getting multiple warnings before the app crashes after idling for a while.

Could this be a regression?

Background Task 1793 ("CKScheduler-com.apple.coredata.cloudkit.activity.export.F7AC7CB7-21C8-4443-BB52-49B93441C5E3"), was created over 30 seconds ago. In applications running in the background, this creates a risk of termination. Remember to call UIApplication.endBackgroundTask(_:) for your task in a timely manner to avoid this.

Still seeing this in iOS 17

Seems like this is back with iOS 18 & 18.1 (so far...). I had to reset everything for my app in the production environment so wiped the iCloud data, reset everything then reinstalled and imported my data into the primary device (iPhone)

Sync completed after a day or so, then I installed the app on the iPad. Sync starts and appears to be doing something then nothing appears to happen for a while (maybe 20 minutes), then the app crashes again. No further data appears to have been downloaded. This happens every time I restart the app.

Sigh...

It's as though it's trying to do 'everything at once' for the initial sync instead of incremental syncs in small batches... and then it drops out.

EDIT: Apple censored the word for 'drops out' that I used (ie. the thing they drop from planes) and substituted it with five asterisks - seriously? What puritanical nonsense is this??

NSPersistentCloudkitContainer Memory Leak -&gt; Crash? (iOS 15 beta 4 &amp; 5)
 
 
Q