Skip to content
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

WPMediaPickerViewController crash: NSInternalInconsistencyException: Invalid batch updates detected #21102

Closed
sentry-io bot opened this issue Jul 13, 2023 · 18 comments · Fixed by wordpress-mobile/MediaPicker-iOS#412

Comments

@sentry-io
Copy link

sentry-io bot commented Jul 13, 2023

Sentry Issue: JETPACK-IOS-FQW

NSInternalInconsistencyException: Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates.
Data source before updates = { 1 section with item counts: [65] }
Data source after updates = { 1 section with item counts: [65] }
Updates = [
	Insert item (0 - 0),
	Insert item (0 - 1),
	Insert item (0 - 2),
	Insert item (0 - 3),
	Insert item (0 - 4),
	Insert item (0 - 5),
	Insert item (0 - 6),
	Insert item (0 - 7),
	Insert item (0 - 8),
	Insert item (0 - 9),
	Insert item (0 - 10),
	Insert item (0 - 11),
	Insert item (0 - 12),
	Insert item (0 - 13),
	Insert item (0 - 14),
	Insert item (0 - 15),
	Insert item (0 - 16),
	Insert item (0 - 17),
	Insert item (0 - 18),
	Insert item (0 - 19),
	Insert item (0 - 20),
	Insert item (0 - 21),
	Insert item (0 - 22),
	Insert item (0 - 23),
	Insert item (0 - 24),
	Insert item (0 - 25),
	Insert item (0 - 26),
	Insert item (0 - 27),
	Insert item (0 - 28),
	Insert item (0 - 29),
	Insert item (0 - 30),
	Insert item (0 - 31),
	Insert item (0 - 32),
	Insert item (0 - 33),
	Insert item (0 - 34),
	Insert item (0 - 35),
	Insert item (0 - 36),
	Insert item (0 - 37),
	Insert item (0 - 38),
	Insert item (0 - 39),
	Insert item (0 - 40),
	Insert item (0 - 41),
	Insert item (0 - 42),
	Insert item (0 - 43),
	Insert item (0 - 44),
	Insert item (0 - 45),
	Insert item (0 - 46),
	Insert item (0 - 47),
	Insert item (0 - 48),
	Insert item (0 - 49),
	Insert item (0 - 50),
	Insert item (0 - 51),
	Insert item (0 - 52),
	Insert item (0 - 53),
	Insert item (0 - 54),
	Insert item (0 - 55),
	Insert item (0 - 56),
	Insert item (0 - 57),
	Insert item (0 - 58),
	Insert item (0 - 59),
	Insert item (0 - 60),
	Insert item (0 - 61),
	Insert item (0 - 62),
	Insert item (0 - 63),
	Insert item (0 - 64)
]
Collection view: <UICollectionView: 0x10d073000; frame = (0 0; 375 603); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280f8d0b0>; backgroundColor = <UIDynamicProviderColor: 0x2803ff3a0; provider = <__NSMallocBlock__: 0x280dd1440>>; layer = <CALayer: 0x2802848c0>; contentOffset: {0, -56}; contentSize: {375, 1599}; adjustedContentInset: {56, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0x10776ff10>; dataSource: <WPMediaPickerViewController: 0x10d07ca00>>
  File "WPMediaPickerViewController.m", line 740, in -[WPMediaPickerViewController updateDataWithRemoved:inserted:changed:moved:]
  File "WPMediaPickerViewController.m", line 137, in __58-[WPMediaPickerViewController registerDataSourceObservers]_block_invoke
  File "MediaLibraryPickerDataSource.m", line 184, in -[MediaLibraryPickerDataSource notifyObserversWithIncrementalChanges:removed:inserted:changed:moved:]
  File "MediaLibraryPickerDataSource.m", line 510, in -[MediaLibraryPickerDataSource controllerDidChangeContent:]
  File "main.swift", line 7, in main
...
(31 additional frame(s) were not displayed)
@wargcm
Copy link
Contributor

wargcm commented Jul 13, 2023

On WordPress, this gets grouped into WORDPRESS-IOS-38W2.

I ran into this issue while smoke testing 22.8.0.1 and could reproduce it every time on my device.

Note: I could not reproduce this issue on a simulator.

Device: iPhone SE (3rd generation)
OS: 16.5.1
Steps:

  • Fresh install
  • Login
  • Tap on the FAB (+)
  • Create a blog post
  • Add an image block
  • Select WordPress Media Library
  • Crash 💥

Video

media_crash.mp4

@develric
Copy link

develric commented Jul 14, 2023

I tried to reproduce it on an iPad 7th gen with iPadOS 16.5.1 and couldn't (don't have an iPhone to double check though but I see in the listed devices for the issue there are iPads so maybe there is some other bit in the testing scenario that I do not have on my side).

@tiagomar
Copy link
Contributor

I tried to reproduce several times on an iPad 9th gen iPadOS 16.3.1 without success, moved to the iPhone 13 iOS 16.5.1 and faced the crash in the first try. Jetpack version was 22.8.0.2 in both devices.

@staskus staskus changed the title NSInternalInconsistencyException: Invalid batch updates detected: the number of sections and/or items returned by the data source b... WPMediaPickerViewController crash: NSInternalInconsistencyException: Invalid batch updates detected Jul 25, 2023
@staskus
Copy link
Contributor

staskus commented Jul 25, 2023

Increasing the priority of the task, since it became the most frequent crash for 22.8 in the last 24 hours and the third most frequent crash for all versions in the last 24 hours.

@salimbraksa salimbraksa self-assigned this Jul 25, 2023
@guarani
Copy link
Contributor

guarani commented Jul 25, 2023

👋 I ran into this just now but in a different flow than the one described above in #21102 (comment). However, I couldn't reproduce it again so here are the steps I recall taking leading up to the crash:

  1. Navigate to the My Site tab
  2. Tap the Media pill button
  3. Tap an image (I recall tapping as the images were loading and the grid layout was being reshuffled as the app fetched the site's media

@staskus
Copy link
Contributor

staskus commented Jul 28, 2023

@salimbraksa

I reproduced the crash by accident on my testing site. It seems to be happening consistently. I invited you as an admin to the site so you can test.

Steps to reproduce:

  1. Fresh install app
  2. Login
  3. My Site
  4. Media
  5. Observe crash
Strack trace
CoreData: fault: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates.
Data source before updates = { 1 section with item counts: [56] }
Data source after updates = { 1 section with item counts: [56] }
Updates = [
	Insert item (0 - 0),
	Insert item (0 - 1),
	Insert item (0 - 2),
	Insert item (0 - 3),
	Insert item (0 - 4),
	Insert item (0 - 5),
	Insert item (0 - 6),
	Insert item (0 - 7),
	Insert item (0 - 8),
	Insert item (0 - 9),
	Insert item (0 - 10),
	Insert item (0 - 11),
	Insert item (0 - 12),
	Insert item (0 - 13),
	Insert item (0 - 14),
	Insert item (0 - 15),
	Insert item (0 - 16),
	Insert item (0 - 17),
	Insert item (0 - 18),
	Insert item (0 - 19),
	Insert item (0 - 20),
	Insert item (0 - 21),
	Insert item (0 - 22),
	Insert item (0 - 23),
	Insert item (0 - 24),
	Insert item (0 - 25),
	Insert item (0 - 26),
	Insert item (0 - 27),
	Insert item (0 - 28),
	Insert item (0 - 29),
	Insert item (0 - 30),
	Insert item (0 - 31),
	Insert item (0 - 32),
	Insert item (0 - 33),
	Insert item (0 - 34),
	Insert item (0 - 35),
	Insert item (0 - 36),
	Insert item (0 - 37),
	Insert item (0 - 38),
	Insert item (0 - 39),
	Insert item (0 - 40),
	Insert item (0 - 41),
	Insert item (0 - 42),
	Insert item (0 - 43),
	Insert item (0 - 44),
	Insert item (0 - 45),
	Insert item (0 - 46),
	Insert item (0 - 47),
	Insert item (0 - 48),
	Insert item (0 - 49),
	Insert item (0 - 50),
	Insert item (0 - 51),
	Insert item (0 - 52),
	Insert item (0 - 53),
	Insert item (0 - 54),
	Insert item (0 - 55)
]
Collection view: <UICollectionView: 0x114849000; frame = (0 0; 393 852); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280c36c40>; backgroundColor = <UIDynamicSystemColor: 0x281650d80; name = systemGroupedBackgroundColor>; layer = <CALayer: 0x2802a52c0>; contentOffset: {0, -97.666666666666671}; contentSize: {393, 1373}; adjustedContentInset: {97.666666666666671, 0, 83, 0}; layout: <UICollectionViewFlowLayout: 0x10f356ae0>; dataSource: <WordPress.MediaLibraryViewController: 0x114850a00>> with userInfo {
    NSAssertFile = "UICollectionView.m";
    NSAssertLine = 10064;
}
2023-07-28 14:18:36.342338+0300 Jetpack[14406:6512994] [error] CoreData: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates.
Data source before updates = { 1 section with item counts: [56] }
Data source after updates = { 1 section with item counts: [56] }
Updates = [
	Insert item (0 - 0),
	Insert item (0 - 1),
	Insert item (0 - 2),
	Insert item (0 - 3),
	Insert item (0 - 4),
	Insert item (0 - 5),
	Insert item (0 - 6),
	Insert item (0 - 7),
	Insert item (0 - 8),
	Insert item (0 - 9),
	Insert item (0 - 10),
	Insert item (0 - 11),
	Insert item (0 - 12),
	Insert item (0 - 13),
	Insert item (0 - 14),
	Insert item (0 - 15),
	Insert item (0 - 16),
	Insert item (0 - 17),
	Insert item (0 - 18),
	Insert item (0 - 19),
	Insert item (0 - 20),
	Insert item (0 - 21),
	Insert item (0 - 22),
	Insert item (0 - 23),
	Insert item (0 - 24),
	Insert item (0 - 25),
	Insert item (0 - 26),
	Insert item (0 - 27),
	Insert item (0 - 28),
	Insert item (0 - 29),
	Insert item (0 - 30),
	Insert item (0 - 31),
	Insert item (0 - 32),
	Insert item (0 - 33),
	Insert item (0 - 34),
	Insert item (0 - 35),
	Insert item (0 - 36),
	Insert item (0 - 37),
	Insert item (0 - 38),
	Insert item (0 - 39),
	Insert item (0 - 40),
	Insert item (0 - 41),
	Insert item (0 - 42),
	Insert item (0 - 43),
	Insert item (0 - 44),
	Insert item (0 - 45),
	Insert item (0 - 46),
	Insert item (0 - 47),
	Insert item (0 - 48),
	Insert item (0 - 49),
	Insert item (0 - 50),
	Insert item (0 - 51),
	Insert item (0 - 52),
	Insert item (0 - 53),
	Insert item (0 - 54),
	Insert item (0 - 55)
]
Collection view: <UICollectionView: 0x114849000; frame = (0 0; 393 852); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280c36c40>; backgroundColor = <UIDynamicSystemColor: 0x281650d80; name = systemGroupedBackgroundColor>; layer = <CALayer: 0x2802a52c0>; contentOffset: {0, -97.666666666666671}; contentSize: {393, 1373}; adjustedContentInset: {97.666666666666671, 0, 83, 0}; layout: <UICollectionViewFlowLayout: 0x10f356ae0>; dataSource: <WordPress.MediaLibraryViewController: 0x114850a00>> with userInfo {
    NSAssertFile = "UICollectionView.m";
    NSAssertLine = 10064;
}
2023-07-28 14:18:36.342872+0300 Jetpack[14406:6512994] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates.
Data source before updates = { 1 section with item counts: [56] }
Data source after updates = { 1 section with item counts: [56] }
Updates = [
	Insert item (0 - 0),
	Insert item (0 - 1),
	Insert item (0 - 2),
	Insert item (0 - 3),
	Insert item (0 - 4),
	Insert item (0 - 5),
	Insert item (0 - 6),
	Insert item (0 - 7),
	Insert item (0 - 8),
	Insert item (0 - 9),
	Insert item (0 - 10),
	Insert item (0 - 11),
	Insert item (0 - 12),
	Insert item (0 - 13),
	Insert item (0 - 14),
	Insert item (0 - 15),
	Insert item (0 - 16),
	Insert item (0 - 17),
	Insert item (0 - 18),
	Insert item (0 - 19),
	Insert item (0 - 20),
	Insert item (0 - 21),
	Insert item (0 - 22),
	Insert item (0 - 23),
	Insert item (0 - 24),
	Insert item (0 - 25),
	Insert item (0 - 26),
	Insert item (0 - 27),
	Insert item (0 - 28),
	Insert item (0 - 29),
	Insert item (0 - 30),
	Insert item (0 - 31),
	Insert item (0 - 32),
	Insert item (0 - 33),
	Insert item (0 - 34),
	Insert item (0 - 35),
	Insert item (0 - 36),
	Insert item (0 - 37),
	Insert item (0 - 38),
	Insert item (0 - 39),
	Insert item (0 - 40),
	Insert item (0 - 41),
	Insert item (0 - 42),
	Insert item (0 - 43),
	Insert item (0 - 44),
	Insert item (0 - 45),
	Insert item (0 - 46),
	Insert item (0 - 47),
	Insert item (0 - 48),
	Insert item (0 - 49),
	Insert item (0 - 50),
	Insert item (0 - 51),
	Insert item (0 - 52),
	Insert item (0 - 53),
	Insert item (0 - 54),
	Insert item (0 - 55)
]
Collection view: <UICollectionView: 0x114849000; frame = (0 0; 393 852); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280c36c40>; backgroundColor = <UIDynamicSystemColor: 0x281650d80; name = systemGroupedBackgroundColor>; layer = <CALayer: 0x2802a52c0>; contentOffset: {0, -97.666666666666671}; contentSize: {393, 1373}; adjustedContentInset: {97.666666666666671, 0, 83, 0}; layout: <UICollectionViewFlowLayout: 0x10f356ae0>; dataSource: <WordPress.MediaLibraryViewController: 0x114850a00>>'
*** First throw call stack:
(0x1ad678cb4 0x1a67143d0 0x1a7e0a56c 0x1afde0af8 0x1af9eb7b4 0x1af949bac 0x106659718 0x106654ad4 0x10412d18c 0x104130448 0x1b4da41a4 0x1b4dc7b6c 0x1b4dc7650 0x1b4da618c 0x1ad6a6590 0x1ad74a828 0x1ad72d8b8 0x1ad6baafc 0x1a7985d38 0x1b4dc002c 0x1b4e25ae0 0x1b4dd1164 0x1b4dbf7d4 0x1b4dc8840 0x1b4dc7b6c 0x10dd2e038 0x10dd3e798 0x10dd3e2dc 0x1ad707c28 0x1ad6e9560 0x1ad6ee3ec 0x1e8b8b35c 0x1afa7b6e8 0x1afa7b34c 0x1052858e4 0x1ccbc6dec)
libc++abi: terminating due to uncaught exception of type NSException


#0	0x00000001ad678cb4 in __exceptionPreprocess ()
#1	0x00000001a67143d0 in objc_exception_throw ()
#2	0x00000001a7e0a56c in -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] ()
#3	0x00000001afde0af8 in -[UICollectionView _Bug_Detected_In_Client_Of_UICollectionView_Invalid_Batch_Updates:] ()
#4	0x00000001af9eb7b4 in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:collectionViewAnimator:] ()
#5	0x00000001af949bac in -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:animationHandler:] ()
#6	0x0000000106659718 in -[WPMediaPickerViewController updateDataWithRemoved:inserted:changed:moved:] at /Users/povilasstaskus/Projects/WordPress-iOS/Pods/WPMediaPicker/Pod/Classes/WPMediaPickerViewController.m:740
#7	0x0000000106654ad4 in __58-[WPMediaPickerViewController registerDataSourceObservers]_block_invoke at /Users/povilasstaskus/Projects/WordPress-iOS/Pods/WPMediaPicker/Pod/Classes/WPMediaPickerViewController.m:137
#8	0x000000010412d18c in -[MediaLibraryPickerDataSource notifyObserversWithIncrementalChanges:removed:inserted:changed:moved:] at /Users/povilasstaskus/Projects/WordPress-iOS/WordPress/Classes/ViewRelated/Media/MediaLibraryPickerDataSource.m:184
#9	0x0000000104130448 in -[MediaLibraryPickerDataSource controllerDidChangeContent:] at /Users/povilasstaskus/Projects/WordPress-iOS/WordPress/Classes/ViewRelated/Media/MediaLibraryPickerDataSource.m:510
#10	0x00000001b4da41a4 in __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke ()
#11	0x00000001b4dc7b6c in developerSubmittedBlockToNSManagedObjectContextPerform ()
#12	0x00000001b4dc7650 in -[NSManagedObjectContext performBlockAndWait:] ()
#13	0x00000001b4da618c in -[NSFetchedResultsController _core_managedObjectContextDidChange:] ()
#14	0x00000001ad6a6590 in __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ ()
#15	0x00000001ad74a828 in ___CFXRegistrationPost_block_invoke ()
#16	0x00000001ad72d8b8 in _CFXRegistrationPost ()
#17	0x00000001ad6baafc in _CFXNotificationPost ()
#18	0x00000001a7985d38 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#19	0x00000001b4dc002c in -[NSManagedObjectContext _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:] ()
#20	0x00000001b4e25ae0 in -[NSManagedObjectContext _postRefreshedObjectsNotificationAndClearList] ()
#21	0x00000001b4dd1164 in -[NSManagedObjectContext _processRecentChanges:] ()
#22	0x00000001b4dbf7d4 in -[NSManagedObjectContext _mergeChangesFromDidSaveDictionary:usingObjectIDs:] ()
#23	0x00000001b4dc8840 in -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] ()

@kean
Copy link
Contributor

kean commented Jul 31, 2023

There was a steep rise of these crashes in 22.8.0.3. I posted in the respective channel p1690817991010719-slack-C012H19SZQ8.

@staskus
Copy link
Contributor

staskus commented Aug 1, 2023

Tested the crash with the site that has a 100% crash rate:

22.7 - No crash
22.8 - Crash
22.9 - Crash
Trunk - Crash

The most apparent difference between 22.7 and 22.8 was an update of WPMediaPicker . This was the only PR between 22.7 and 22.8.

I reverted the change in the media picker and re-tested locally - No crash

Fix crash when displaying an empty view is a root cause for this crash.

@hassaanelgarem
Copy link
Contributor

@staskus can you invite me to your site? 🙏

@staskus
Copy link
Contributor

staskus commented Aug 1, 2023

@staskus can you invite me to your site? 🙏

@hassaanelgarem of course, done! I tested on iPhone 14 Pro and after fresh install I get a crash 100%. From the first look I couldn't understand why that particular fix could result in a crash in this case.

@salimbraksa
Copy link
Contributor

I tested on trunk and simulator but couldn't reproduce the crash. @staskus do you confirm these are the reproduction steps?

CleanShot.2023-08-01.at.11.55.20.mp4

@staskus
Copy link
Contributor

staskus commented Aug 2, 2023

There're some indications that it could be related to iOS updates iOS 16.4.1 - UICollectionViewController crashes

Apple Frameworks Engineer indicates:

This particular exception is new as of iOS 16.4. It indicates a bug in either:

  • your implementation of batch updates (e.g. inserts, deletes, and moves you perform on the UICollectionView), or
  • the counts (number of sections, and number of items in each section) returned by your data source.

When you are performing batch updates manually yourself, make sure to always use performBatchUpdates (documentation) and advance your data source to the new state inside the updates closure. Your data source must return the old counts up until the updates closure executes, and must return the new counts after the updates closure finishes executing:

Possibly related discussions: pbzQyC-3U2-p2, pbzQyC-3SP-p2

@guarani
Copy link
Contributor

guarani commented Aug 12, 2023

Sentry reports two further crashes in 22.9. Could you please take a look @staskus and re-open this if necessary?

@staskus
Copy link
Contributor

staskus commented Aug 14, 2023

Could you please take a look @staskus and re-open this if necessary?

@guarani, okay, I will!

@staskus
Copy link
Contributor

staskus commented Aug 14, 2023

@guarani

These are rarer crashes happening in a bit different place. There were most crashes in performBatchUpdates but some happen when calling reloadItemsAtIndexPath.

I applied the same temporary fix, we can try merging them to release/23.0:

@guarani
Copy link
Contributor

guarani commented Aug 31, 2023

Sentry says this issue regressed but the only occurrence in 23.0 seems like a different crash. Instead of Invalid batch updates detected it's UITableView internal inconsistency: _visibleRows and _visibleCells must be of same length. The priority to fix seems very low since it's just one occurrence, so just sharing here for visibility.

@staskus
Copy link
Contributor

staskus commented Sep 1, 2023

@guarani thanks!

With the integration of native picker (#21190) we should soon have a more stable solution available to our users. Moreover, given only a single occurrence, we shouldn't spend time investigating the issue further.

@guarani
Copy link
Contributor

guarani commented Sep 1, 2023

With the integration of native picker (#21190) we should soon have a more stable solution available to our users.

I noticed that this last occurrence seems to be Reader or Notification so it's probably wrongly grouped by Sentry. There are some ideas for changing the grouping behavior here, https://docs.sentry.io/product/data-management-settings/event-grouping/, but I haven't explored it.

Moreover, given only a single occurrence, we shouldn't spend time investigating the issue further.

I agree 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment