On iOS 10, NSFetchedResultsController leaks 'open file descriptors'

Originator:mbd
Number:rdar://28361550 Date Originated:19/09-2016
Status:Closed Resolved:17/11-2016
Product:iOS Product Version:10
Classification:Serious bug Reproducible:Always
 
Summary:
After mutating the fetchRequest on a fetchedResultsController, each save of the managed object context will open one or more file descriptors pointing to the sectionInfo cache file of the fetchedResultsController.
When hitting 255 open files, no new resources may be opened (on iOS devices), causing any subsequent opening of xib files, images, etc. to fail.

Steps to Reproduce:
1. Build the project in Xcode 8
2. Install on an iPad running iOS 10
3. Launch the app.
4. Tap 'Add Entity' a few times
5. Tap 'Mutate Fetch Request' once
6. Continue tapping 'Save Context (x 20)' until 255 file descriptors are open (they will be shown in a UITextView)
7. Tap 'Load Resource' - just to see that the loading of the resource will fail.
8. The force-unwrapping of the resource causes the app to crash. This is intentional to show problems that are likely to occur as a side effect of this issue.

Expected Results:
NSFetchedResultsController section cache does not leak file descriptors.

Actual Results:
NSFetchedResultsController section cache leaks file descriptors.

Version:
iOS 10.0.1

Notes:
This happens in simulator as well, but on MacOS you can open 1024 files without running into the issue.
This also happens with apps built using Xcode 7 but running on iOS 10.

Configuration:
iPad Pro 12.9 inc

Link to project demonstrating the issue:
https://github.com/mortenbekditlevsen/CoreDataFDLeak

Comments

Using XCode 8.1 and simulator iPad Air 10.1 there's no need to mutate fetch request. Crash occurs simply installing app, saving context until 255 and loading resource. Both on device and simulator, crash happens after 255 for me (not 1024 on the simulator). Disabling NSFetchedResultsController caching with nil as cachename solves the problem, but we can't get advantage of caching.

Console shows this error message at every save context (255x): [error] error: (NSFetchedResultsController): couldn't read cache file to update store info timestamps

The problem occurs also for apps already on appstore (built with xcode 7) on devices upgraded to iOS 10. That's a very serious bug!

Added on Apple Bug Reporter.

EDIT On iOS 10.2 beta 1 the bug does not occur: it has been solved (for now).


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!