Repository navigation
Prevent crashes when retained posts are deleted - #26015
Conversation
|
| App Name | WordPress | |
| Configuration | Release-Alpha | |
| Build Number | 34609 | |
| Version | PR #26015 | |
| Bundle ID | org.wordpress.alpha | |
| Commit | 82686ec | |
| Installation URL | 4eq51ubgkov7g |
|
| App Name | Jetpack | |
| Configuration | Release-Alpha | |
| Build Number | 34609 | |
| Version | PR #26015 | |
| Bundle ID | com.jetpack.alpha | |
| Commit | 82686ec | |
| Installation URL | 6348lstkaecn8 |
|
Version |
| try await operation.perform(on: subject, using: repository) | ||
| } | ||
| #expect(factory.callCount == 0) | ||
| } |
There was a problem hiding this comment.
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.
| } | |
| } | |
| @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") | |
| } |
There was a problem hiding this comment.
I have revert this call order change in 82686ec
Co-authored-by: Jeremy Massel <1123407+jkmassel@users.noreply.github.com>
wpmobilebot
left a comment
There was a problem hiding this comment.
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.
- Load the posts list so the post is cached.
- Relaunch, so the delayed refresh is in flight.
- Open ⋯ → Move to trash, and leave the alert up.
- Permanently delete the post through the REST API.
- The delayed refresh purges the post behind the alert.
- Confirm the trash.
Results:
- Trunk: the app crashes with
EXC_BAD_ACCESS(KERN_INVALID_ADDRESS at 0x0) inPostServiceRemoteFactory.forBlog(_:), called fromPostRepository.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:
PostRepositoryPostLifetimeTestspasses, with 3 tests and 7 cases.PostRepositoryTests,PostRepositorySaveTests,PostRepositoryPostsListTestsandPostCoordinatorTestspass: 50 tests, 0 failures.- With trunk's
PostRepository.swift,trashAfterBackgroundPurgefails atfactory.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 withNSBatchDeleteRequest, 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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:
PostRepositoryPostLifetimeTestspasses: 4 tests, including the newpostUnavailableMessage.PostRepositoryTests,PostRepositorySaveTests,PostRepositoryPostsListTestsandPostCoordinatorTestspass: 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.
🤖 Build Failure AnalysisThis build has failures. Claude has analyzed them - check the build annotations for details. |
wpmobilebot
left a comment
There was a problem hiding this comment.
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.





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:
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).