PerformBatchUpdates crashes with NSInternalInconsistencyException requesting absurd index

Originator:smiller
Number:rdar://31749591 Date Originated:20-Apr-2017 06:49 PM
Status:Open Resolved:
Product:iOS + SDK Product Version:All iPhone and iOS versions
Classification:Crash Reproducible:Unable
 
Area:
UIKit

Summary:
Please see the included stack traces for this bug.

We are unable to reproduce this crash, but it has started occurring in as high as 0.1% of sessions. While we do not have reproduction steps, we know that when using `UICollectionView.reloadSections` instead of `UICollectionView.reloadData` this crash does not occur. 

Because `UICollectionView.reloadSections` instead of `UICollectionView.reloadData` fixes the crashes we suspect that this is similar to the bug filed in Bug Report #31748196 (https://openradar.appspot.com/31748196).

Steps to Reproduce:
Unknown. We have been unsuccessful in attempts to reproduce this bug.

It seems that a similar crash has been reported in bug report #27354858, https://openradar.appspot.com/27354858 which has reproduction steps. However, we do not use `insertItems` in the `performBatchUpdates` or elsewhere in our code. We do use `insertSections` but only once at the beginning of our `UICollectionView` lifecycle.

Expected Results:
No crashes.

Actual Results:
Crashes.

Version:
All iOS versions, it seems there is a significantly reduced number of crashes on iOS 10.3

Notes:


Configuration:
All iPhone devices, there seems to be no correlation to hardware.

Attachments:
'ExampleCrashStackTraces_PerformBatchUpdates.zip' was successfully uploaded.

Added for Open Radar:
Here are some examples of the tops of included stack traces

```
Exception Type:      EXC_CRASH (SIGABRT)
Exception Codes:     0x00000000 at 0x0000000000000000
Crashed Thread:      0
Application Specific Information: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for index path for global index 4361310367 when there are only 1 items in the collection view'

Thread 0 Crashed:
0   CoreFoundation                 0x000000018e38d1b8 __exceptionPreprocess + 124
1   libobjc.A.dylib                0x000000018cdc455c objc_exception_throw + 56
2   CoreFoundation                 0x000000018e38d08c -[NSException raise:format:] + 0
3   Foundation                     0x000000018ee4502c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 112
4   UIKit                          0x00000001944147f0 -[UICollectionViewData indexPathForItemAtGlobalIndex:] + 300
5   UIKit                          0x0000000194b3c7e8 -[UICollectionView _viewAnimationsForCurrentUpdate] + 220
6   CoreFoundation                 0x000000018e26e17c -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 136
7   CoreFoundation                 0x000000018e26e048 -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 180
8   UIKit                          0x0000000194418064 -[UICollectionView _viewAnimationsForCurrentUpdate] + 160
9   UIKit                          0x00000001944155dc -[UICollectionView _viewAnimationsForCurrentUpdate] + 320
10  UIKit                          0x0000000194b3f200 __71-[UICollectionView _updateWithItems:tentativelyForReordering:animator:]_block_invoke.1993 + 132
11  UIKit                          0x00000001942507dc +[UIView(Animation) performWithoutAnimation:] + 104
12  UIKit                          0x0000000194b3e230 -[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3224
13  UIKit                          0x0000000194b3c2d0 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 13816
14  UIKit                          0x0000000194b3fc98 -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 92
15  UIKit                          0x0000000194b3ff78 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 384
16  UIKit                          0x0000000194b3fdd8 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 96
17  UIKit                          0x0000000194b3fd5c -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 84
18  UIKit                          0x000000019441c618 -[UICollectionView performBatchUpdates:completion:] + 64
```

```
Exception Type:      EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:     KERN_INVALID_ADDRESS at 0x20b40628
Crashed Thread:      0

Thread 0 Crashed:
0   UIKit                          0x000000002ed87762 -[UICollectionViewData layoutAttributesForItemAtIndexPath:] + 214
1   UIKit                          0x000000002ed84377 -[UICollectionViewData globalIndexForItemAtIndexPath:] + 67
2   UIKit                          0x000000002eee5861 -[UICollectionView _viewAnimationsForCurrentUpdate] + 3361
3   UIKit                          0x000000002f32c94b -[UICollectionView _updateWithItems:tentativelyForReordering:] + 1747
4   UIKit                          0x000000002f32b343 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:] + 8563
5   UIKit                          0x000000002f32d92f -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 407
6   UIKit                          0x000000002f32d793 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 31
7   UIKit                          0x000000002eeea617 -[UICollectionView performBatchUpdates:completion:] + 31
```

```
Exception Type:      EXC_CRASH (SIGABRT)
Exception Codes:     0x00000000 at 0x0000000000000000
Crashed Thread:      0
Application Specific Information: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for index path for global index 6249663710 when there are only 2 items in the collection view'

Thread 0 Crashed:
0   CoreFoundation                 0x00000001907d91b8 __exceptionPreprocess + 124
1   libobjc.A.dylib                0x000000018f21055c objc_exception_throw + 56
2   CoreFoundation                 0x00000001907d908c -[NSException raise:format:] + 0
3   Foundation                     0x000000019129102c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 112
4   UIKit                          0x00000001968607f0 -[UICollectionViewData indexPathForItemAtGlobalIndex:] + 300
5   UIKit                          0x0000000196f887e8 -[UICollectionView _viewAnimationsForCurrentUpdate] + 220
6   CoreFoundation                 0x00000001906ba17c -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 136
7   CoreFoundation                 0x00000001906ba048 -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 180
8   UIKit                          0x0000000196864064 -[UICollectionView _viewAnimationsForCurrentUpdate] + 160
9   UIKit                          0x00000001968615dc -[UICollectionView _viewAnimationsForCurrentUpdate] + 320
10  UIKit                          0x0000000196f8b200 __71-[UICollectionView _updateWithItems:tentativelyForReordering:animator:]_block_invoke.1993 + 132
11  UIKit                          0x000000019669c7dc +[UIView(Animation) performWithoutAnimation:] + 104
12  UIKit                          0x0000000196f8a230 -[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3224
13  UIKit                          0x0000000196f882d0 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 13816
14  UIKit                          0x0000000196f8bc98 -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 92
15  UIKit                          0x0000000196f8bf78 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 384
16  UIKit                          0x0000000196f8bdd8 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 96
17  UIKit                          0x0000000196f8bd5c -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 84
18  UIKit                          0x0000000196868618 -[UICollectionView performBatchUpdates:completion:] + 64
```

```
Exception Type:      EXC_CRASH (SIGABRT)
Exception Codes:     0x00000000 at 0x00000000
Crashed Thread:      0
Application Specific Information: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for index path for global index 1102011508 when there are only 1 items in the collection view'

Thread 0 Crashed:
0   CoreFoundation                 0x00000000235a391b __exceptionPreprocess + 127
1   libobjc.A.dylib                0x0000000022d3ee17 objc_exception_throw + 39
2   CoreFoundation                 0x00000000235a37f1 -[NSException raise:format:] + 1
3   Foundation                     0x0000000023d87529 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 93
4   UIKit                          0x0000000027d01909 -[UICollectionViewData indexPathForItemAtGlobalIndex:] + 273
5   UIKit                          0x0000000027d00ae9 -[UICollectionViewLayout prepareForCollectionViewUpdates:] + 533
6   UIKit                          0x0000000027d008c7 -[UICollectionView _prepareLayoutForUpdates] + 323
7   UIKit                          0x000000002832094b -[UICollectionView _updateWithItems:tentativelyForReordering:] + 2275
8   UIKit                          0x000000002831eff9 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:] + 10393
9   UIKit                          0x0000000028321d59 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 353
10  UIKit                          0x0000000028321be5 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 57
11  UIKit                          0x0000000027d089e1 -[UICollectionView performBatchUpdates:completion:] + 45
```

Comments

Apple Developer Relations

Please verify this issue with the iOS 11 GM and update your bug report at https://bugreport.apple.com/ with your results.

iOS 11 GM (15A372) https://developer.apple.com/download/ Posted Date: Sep 19th, 2017

If the issue persists, please attach a new sysdiagnose captured in the latest build and attach it to the bug report. Thank you.

iOS sysdiagnose Instructions: https://developer.apple.com/services-account/download?path=/iOS/iOS_Logs/sysdiagnose_Logging_Instructions.pdf


For a complete list of logging instructions visit: https://developer.apple.com/bug-reporting/profiles-and-logs/


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!