Skip to content

Fix a crash when a post or page list changes during a long press - #26416

Merged
crazytonyli merged 5 commits into
trunkfrom
bugfix/post-context-menu-stale-row
Nov 26, 2026
Merged

crazytonyli merged 5 commits into
trunkfrom
bugfix/post-context-menu-stale-row

Conversation

@crazytonyli

@crazytonyli crazytonyli commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes JETPACK-IOS-1C5W, WORDPRESS-IOS-498X, JETPACK-IOS-1JHW and JETPACK-IOS-1K24.

UIKit builds a row's context menu about half a second after a long press starts. The Posts list, post search and Pages list read the row again at that point. If a sync removed rows in between, the app crashed or the menu acted on the next post. This change keeps the pressed post when the press starts, finds its current row when the menu is built, and opens no menu if the post is gone. New unit tests change the list between the two calls.

To test:

  1. In Posts, scroll past row 40. Switch to Reader and back, and long-press a row while the list refreshes. Expect no crash.
  2. Trash a post and long-press the row below it as the trash completes. Expect View to open the pressed post.

UIKit builds a row's context menu about half a second after the long press
starts. The Posts, Search and Pages lists read the row again at that point,
so a sync that removed rows in between crashed the app or opened the menu
for the next post. Keep the pressed post when the press starts, find its
current row when building the menu, and show no menu if it is gone.
@crazytonyli
crazytonyli requested a review from jkmassel November 18, 2026 01:41
@crazytonyli crazytonyli added this to the 27.5 milestone Nov 18, 2026
@crazytonyli
crazytonyli marked this pull request as ready for review November 18, 2026 01:41
@wpmobilebot wpmobilebot modified the milestones: 27.5, 27.6 Nov 20, 2026
@wpmobilebot

Copy link
Copy Markdown
Contributor

Version 27.5 has now entered code-freeze, so the milestone of this PR has been updated to 27.6.

return nil
}
let cell = self.tableView.cellForRow(at: indexPath)
return AbstractPostMenuHelper(post).makeMenu(presentingView: cell ?? UIView(), delegate: self)

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.

Finding from Claude:
Not blocking: if a sync pushes the pressed post off screen before the menu opens, the menu has no actions.

What happens: the menu now looks up the cell at the post's new row. When that row is off screen there's no cell, so cell ?? UIView() passes a throwaway view. AbstractPostMenuHelper holds it weakly, so it's gone by the time the deferred element runs, and no actions are added.

Who is affected: rare. You'd need to press a row near the bottom while a sync inserts enough posts above it. Before this PR, the same case showed a menu for the wrong post. The page list and search use the same fallback.

Suggested fix: keep the pressed cell and fall back to it, then to the table view:

let pressedCell = tableView.cellForRow(at: indexPath)
// in the closure, with [weak pressedCell]:
let cell = self.tableView.cellForRow(at: indexPath) ?? pressedCell
return AbstractPostMenuHelper(post).makeMenu(presentingView: cell ?? self.tableView, delegate: self)
How this was checked

A test added to PostListContextMenuTests inserted 30 newer posts after the press, which moved the pressed post from row 1 to row 31 (rows 0–18 visible). "View" was missing from the menu in 3 of 3 runs. With the fix above applied to the post list, that test and the existing three pass (iPhone 18 Pro, iOS 27.2). The page list and search fallbacks were read, not tested.

If a sync moves the pressed post off screen before UIKit builds the menu,
the post has no cell to anchor the menu, and UIKit lifts the row now at
the pressed position, which shows another post. The menu was stuck on
"Loading…" with no actions. Show no menu instead, as when the post is
gone.
@crazytonyli
crazytonyli added this pull request to the merge queue Nov 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to invalid changes in the merge commit Nov 26, 2026
@dangermattic

Copy link
Copy Markdown
Collaborator
1 Warning
⚠️ This PR is larger than 500 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.

Generated by 🚫 Danger

@crazytonyli
crazytonyli added this pull request to the merge queue Nov 26, 2026
Merged via the queue into trunk with commit 6e51d61 Nov 26, 2026
21 checks passed
@crazytonyli
crazytonyli deleted the bugfix/post-context-menu-stale-row branch November 26, 2026 08:18
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.

4 participants