Skip to content

Fix a crash when Stats details list the same entry twice - #26464

Open
crazytonyli wants to merge 1 commit into
trunkfrom
bugfix/stats-duplicate-top-level-rows
Open

crazytonyli wants to merge 1 commit into
trunkfrom
bugfix/stats-duplicate-top-level-rows

Conversation

@crazytonyli

Copy link
Copy Markdown
Contributor

Description

Fixes JETPACK-IOS-1KAX

To reproduce the issue: with New Stats turned off, open Insights > Tags and Categories > View more on a site whose stats list the same category twice, with the same name, views and link. The app crashes with "supplied item identifiers are not unique" as soon as the list loads. Clicks, Authors and Referrers details crash the same way when an entry is repeated. The Insights card shows both copies without crashing.

In the Sentry events the repeated entry is always a single category with a ?taxonomy=category&term=<slug> link on a custom domain. Reading the WordPress.com code, the endpoint builds Jetpack sites' term links from the slug only and drops the term ID, so two category terms with the same name and slug come out as identical groups. That part is a hypothesis; nobody checked an affected site's terms.

Each top-level row now also compares how many equal rows come before it, so both copies show. That matches what the card already does, and what the details list does when the two copies' views differ. Dropping or merging the copy was ruled out: if the server explanation holds, they are two real terms. When one copy's count changes, only that row is replaced.

One case is still open: expanding a repeated entry that has two or more children expands both copies, and their children collide. The fix for that belongs in the child row identity that #26462 changes, so it can follow once that lands. The two PRs touch the same files but not the same lines, and they merge cleanly in either order.

What I ran

  • I built this change and ran the affected tests locally

Claude Code ran these on an iOS 27.2 simulator:

  • WordPressTest/SiteStatsDetailsDuplicateRowsTests and WordPressTest/SiteStatsDetailsPostStatsTests: Test run with 6 tests in 2 suites passed. The three new tests fail on trunk.
  • WordPressTest/SiteStatsInsightsDetailsViewModelTests, WordPressTest/SiteStatsImmuTableRowsTests and WordPressTest/StatsTotalRowDataTests: Executed 6 tests, with 0 failures.

Testing instructions

Claude Code reproduced the crash and checked the fix in the Jetpack app with stats responses served from a local proxy. None of the test account's sites has tag stats, so this wasn't tested with real data.

When a Stats response listed the same entry twice, with the same name,
count and link, the details table crashed on the equal rows. Each
top-level row now also carries how many equal rows come before it, so
both copies show, as they already do when their counts differ.
@crazytonyli
crazytonyli marked this pull request as ready for review November 26, 2026 22:18
@crazytonyli
crazytonyli requested a review from jkmassel November 26, 2026 22:18
@crazytonyli crazytonyli added this to the 27.6 milestone Nov 26, 2026

@wpmobilebot wpmobilebot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: request changes. Numbering the copies fixes the reported crash: the details list now loads and shows both copies, and the Sentry case is a single category with no children. But tapping a repeated entry that has two or more children still crashes with the same exception. Before this PR the same data crashed as soon as the list opened, so no user is worse off. The tap crash is only reachable with this PR, though, and a crash counts as a blocker. Details are inline.

Tested on an iPhone 18 Pro simulator, iOS 27.2, Jetpack Debug build of 3a268a5, New Stats off, signed in with the WP.com test account. Tag stats came from a review-only store patch: Garden ×2 (single category), "Travel, Food" ×2 (two tags), plus other rows. Steps: Stats > Insights > add the Tags and Categories card > View more. The list loads with both copies, and alignment, spacing, separators and bars look right (light mode):

Tests: the PR's SiteStatsDetailsDuplicateRowsTests (3) and SiteStatsInsightsDetailsViewModelTests (3) pass on the PR head, and all three new tests fail on the merge base. I added a review test showing that SiteStatsInsightsDetailsViewModel's copy of the change also works: a repeated category loads there, and it throws on the merge base.

Gaps: I didn't check dark mode, Dynamic Type or VoiceOver. The data was synthetic (no real affected site), and I didn't check the Clicks, Authors or Referrers screens in the app; they share the code path the unit tests cover.

hideIndentedSeparator: hideIndentedSeparator,
hideFullSeparator: !isLastRow,
expanded: expanded)
row.copyIndex = earlierRowsByName[rowData.name, default: []].count { $0 == rowData }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocker: Expanding a repeated entry that has two or more children still crashes. rowExpanded looks up the row's name, so tapping either copy expands both. Each copy then appends the same DetailExpandableChildRows (and nested parentRow(rowData: childRowData…) rows), and those have no copyIndex. With two or more children the first ones match exactly, and the snapshot hits the same duplicate-identifier exception.

On trunk this data crashed when the list opened, so nobody is worse off. But the tap crash is only reachable with this PR, so the release note's "Fix a crash when opening … details that list the same entry twice" holds only until the user taps.

Evidence:

  • App: in the build from the summary, tapping either "Travel, Food" row aborts with NSInternalInconsistencyException in -[__UIDiffableDataSourceSnapshot appendItemsWithIdentifiers:intoSectionWithIdentifier:], via StatsTotalRow.didTapDisclosureButton → SiteStatsDetailTableViewController.toggleChildRowsForRow → SiteStatsDetailsViewModel.tableViewSnapshot.
  • Unit test: using the same table setup as SiteStatsDetailsDuplicateRowsTests, a repeated two-tag group loads collapsed. After StatsDataHelper.expandedRowLabelsDetails[.insightsTagsAndCategories] = ["Travel, Food"], tableViewSnapshot() throws NSInternalInconsistencyException (_UIDiffableDataSourceUpdate.m:81).

The same code is in SiteStatsInsightsDetailsViewModel at line 1196. A small fix that doesn't need #26462: pass the parent's copyIndex to the child and nested parent rows it appends, and add copyIndex to DetailExpandableChildRow's ==. Then the two expanded copies stay distinct, even though both still expand together.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants