Skip to content

Prevent crashes when retained posts are deleted - #26015

Merged
crazytonyli merged 7 commits into
trunkfrom
bugfix/sentry-post-lifetime
Nov 27, 2026
Merged

crazytonyli merged 7 commits into
trunkfrom
bugfix/sentry-post-lifetime

Conversation

@crazytonyli

@crazytonyli crazytonyli commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

The app may crash when delete/trash an post that's already deleted on the server but yet to be synced.

Here is the easiest way to reproduce the crash. It may not exactly match what's happening to the real users, but the root cause is the same:

  1. Simulate a slow network, go to the app's posts list. The posts are being refreshed in the background, slowly.
  2. Tap more actions on a post, and select Trash. You'll see an alert. Leave the alert on.
  3. On the web, delete the selected post.
  4. The app's post list is refreshed.
  5. Confirm the trash post alert. App crashes.

Sentry records a new issue for this crash (i.e., https://a8c.sentry.io/issues/7703437016/?project=1438083), but it's not actually new (i.e., https://a8c.sentry.io/issues/7398708809/?project=1438083).

@crazytonyli
crazytonyli marked this pull request as ready for review September 14, 2026 06:59
@crazytonyli crazytonyli added this to the 27.3 milestone Sep 14, 2026
@wpmobilebot

wpmobilebot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number34609
VersionPR #26015
Bundle IDorg.wordpress.alpha
Commit82686ec
Installation URL4eq51ubgkov7g
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

wpmobilebot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number34609
VersionPR #26015
Bundle IDcom.jetpack.alpha
Commit82686ec
Installation URL6348lstkaecn8
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot wpmobilebot modified the milestones: 27.3, 27.4 Sep 20, 2026
@wpmobilebot

Copy link
Copy Markdown
Contributor

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

@crazytonyli
crazytonyli changed the base branch from trunk to release/27.3 September 21, 2026 06:51
@crazytonyli crazytonyli modified the milestones: 27.4, 27.3 ❄️ Sep 22, 2026
@dangermattic

dangermattic commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator
1 Warning
⚠️ This PR is assigned to the milestone 27.3. The due date for this milestone has already passed.
Please assign it to a milestone with a later deadline or check whether the release for this milestone has already been finished.

Generated by 🚫 Danger

try await operation.perform(on: subject, using: repository)
}
#expect(factory.callCount == 0)
}

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.

Moving getRemoteService above deleteAllRevisions() means a trash that fails because no remote is available now keeps the user's unsynced revisions. Nothing asserts that directly – reverting the order only fails rejectsMissingBlog(.trash) incidentally, by crashing the test process on the blog is a required value validation error. This test fails cleanly on the three revision expectations with the old order, and passes with this PR.

Suggested change
}
}
@Test
func trashKeepsRevisionsWhenRemoteIsUnavailable() async throws {
let stack = ContextManager.forTesting()
let post = makePost(in: stack)
let revision = post.createRevision()
revision.postTitle = "Unsynced edit"
stack.saveContextAndWait(stack.mainContext)
let factory = RemoteFactorySpy()
let repository = PostRepository(coreDataStack: stack, remoteFactory: factory)
await #expect(throws: PostRepository.Error.self) {
try await repository.trash(post)
}
#expect(factory.callCount == 1)
// A failed trash must not discard local edits that were never synced.
#expect(post.revision === revision)
#expect(revision.managedObjectContext != nil)
#expect(revision.postTitle == "Unsynced edit")
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have revert this call order change in 82686ec

crazytonyli and others added 2 commits September 23, 2026 11:20
Co-authored-by: Jeremy Massel <1123407+jkmassel@users.noreply.github.com>
@crazytonyli
crazytonyli changed the base branch from release/27.3 to trunk September 27, 2026 19:41

@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.

Approve. The fix works: trashing a post that a list refresh already purged no longer crashes the app.

Device: iPhone 18 Pro simulator, iOS 27.2, Jetpack Debug build, WordPress.com test site. I tested trunk (a6949dc6c3) and the PR head (c8f8a8df22).

Repro (from the PR description): I couldn't slow the simulator's network, so the local builds only add a 45-second sleep before the post list's getPosts request, enabled by a launch argument.

  1. Load the posts list so the post is cached.
  2. Relaunch, so the delayed refresh is in flight.
  3. Open ⋯ → Move to trash, and leave the alert up.
  4. Permanently delete the post through the REST API.
  5. The delayed refresh purges the post behind the alert.
  6. Confirm the trash.

Results:

  • Trunk: the app crashes with EXC_BAD_ACCESS (KERN_INVALID_ADDRESS at 0x0) in PostServiceRemoteFactory.forBlog(_:), called from PostRepository.trash(_:) (PostRepository.swift:298).
  • PR: no crash. The purged row is gone and an error alert is shown (see the inline Suggestion).
Alert open while refresh is in flight After confirming (PR)

Tests:

  • PostRepositoryPostLifetimeTests passes, with 3 tests and 7 cases.
  • PostRepositoryTests, PostRepositorySaveTests, PostRepositoryPostsListTests and PostCoordinatorTests pass: 50 tests, 0 failures.
  • With trunk's PostRepository.swift, trashAfterBackgroundPurge fails at factory.callCount == 0, so it guards the fix. It fails cleanly rather than crashing because the spy never dereferences the nil blog.

Gaps:

  • I did not exercise permanent delete from the Trashed tab, save/update, or autosave end-to-end. The unit tests cover them.
  • I did not test the WordPress app or a self-hosted site.
  • The Pages list refresh (fetchAllPages) removes pages with NSBatchDeleteRequest, which doesn't update the main context. This guard therefore likely doesn't cover pages. That is pre-existing and I didn't verify it.

!blog.isDeleted, blog.managedObjectContext != nil,
let remote = remoteFactory.forBlog(blog)
else {
throw Error.remoteAPIUnavailable

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.

Suggestion: In the repro, the purged post now hits this branch. The user sees an alert with "Something went wrong" as both the title and the message (screenshot in the review summary), even though the post is already gone on the server. Trunk shows PostSaveError.deleted ("… was permanently deleted") when the server returns 404. A dedicated error, or treating this case as already deleted, would avoid a misleading failure alert. That can be a follow-up; the crash fix is the priority.

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.

Resolved. In b3c1f4b the purged-post case throws the new PostRepository.Error.postUnavailable. I reran the same repro on the iPhone 18 Pro simulator (iOS 27.2): open the trash alert, delete the post on the server, let the delayed list refresh purge it, then confirm. The app doesn't crash, and the alert now explains the problem instead of repeating the generic text:

Optional wording point: by the time this alert shows, the refresh has already removed the row, so the user can't "try again" on that post.

guard let remote = try getRemoteService(for: revision) as? PostServiceRemoteREST else {
throw Error.remoteAPIUnavailable
}
assert(revision.isRevision())

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.

Nit: Moving assert(revision.isRevision()) below the remote lookup means a self-hosted (non-REST) caller that passes an original post now gets remoteAPIUnavailable and never reaches the assert. If the move is only so a deleted revision does not trip the assert, assert(revision.managedObjectContext == nil || revision.isDeleted || revision.isRevision()) at the top would keep the check. Once the deletion is saved, isDeleted is already false again, so the context check is needed. Optional.

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.

Not resolved. In b3c1f4b, assert(revision.isRevision()) is still after the remote lookup. It's optional, so it doesn't block.

A list refresh can remove a post while an action on it is pending. Report that case with its own error and message instead of the generic one. The post may still exist on the site, so the message does not claim it was deleted.

@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.

Approve (re-review of b3c1f4b). The crash fix still holds. The new postUnavailable error replaces the generic message for a purged post, which resolves my Suggestion. My optional Nit about the autosave assert is still open.

Device: iPhone 18 Pro simulator, iOS 27.2, Jetpack Debug build.

Steps: the same repro as round one, with the delayed list refresh as a local-only test aid. Open the trash alert, delete the post on the server, let the refresh purge it, then confirm. The app doesn't crash, and the alert reads "This post is no longer available on this device. Refresh the list and try again." The text isn't truncated and the layout is fine.

Tests:

  • PostRepositoryPostLifetimeTests passes: 4 tests, including the new postUnavailableMessage.
  • PostRepositoryTests, PostRepositorySaveTests, PostRepositoryPostsListTests and PostCoordinatorTests pass: 50 tests, 0 failures.
  • CI was still running when I submitted.

Gaps: the same as round one. I didn't test the WordPress app, a self-hosted site, or the Pages list.

@wpmobilebot

Copy link
Copy Markdown
Contributor

🤖 Build Failure Analysis

This build has failures. Claude has analyzed them - check the build annotations for details.

@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.

Approve (re-review of 407e665). The only new commit is a trunk merge. Its only change to the PR's files is trunk's swift-log migration of one DDLogWarn call in PostRepository.swift, which doesn't touch the fix. My review threads have no new replies: the Suggestion is still resolved, and the optional autosave assert Nit is still open.

CI: Unit Tests failed on the previous head, b3c1f4b (build 36510). On this head, Unit Tests and all other checks pass (build 36515), so there's no CI blocker.

Local: I ran the full WordPressTest target on this head on an iPhone 18 Pro simulator (iOS 27.2). It passes: 1,558 XCTest tests and 818 Swift Testing tests, including PostRepositoryPostLifetimeTests. One PinghubWebSocketTests timeout was retried and passed; it's unrelated to this PR.

Not re-run: I didn't repeat the simulator repro because the merge doesn't change the trash path. The round-two repro on b3c1f4b still applies. The gaps are the same as before: the WordPress app, self-hosted sites and the Pages list.

@crazytonyli
crazytonyli dismissed jkmassel’s stale review November 27, 2026 01:20

Issues all resolved.

@crazytonyli
crazytonyli added this pull request to the merge queue Nov 27, 2026
Merged via the queue into trunk with commit af743dd Nov 27, 2026
21 checks passed
@crazytonyli
crazytonyli deleted the bugfix/sentry-post-lifetime branch November 27, 2026 01:32
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