Guard against nil reply in createReplyForComment callback - #25964
Open
jkmassel wants to merge 2 commits into
Open
Guard against nil reply in createReplyForComment callback#25964jkmassel wants to merge 2 commits into
jkmassel wants to merge 2 commits into
Conversation
createReplyForComment's completion is annotated non-null but its implementation can pass nil: it re-resolves the reply on the main context via existingObjectWithID:error:, which returns nil when the optimistic comment can't be resolved again (a rare Core Data race). Annotate the callback _Nullable so it imports into Swift as Comment?, and guard the single caller so a nil reply resumes with a failure instead of forwarding nil into uploadComment. Adding the guard makes that completion a multi-statement closure, so pin the continuation type explicitly (UnsafeContinuation<Void, Error>) to keep T inferable.
Contributor
|
| App Name | WordPress | |
| Configuration | Release-Alpha | |
| Build Number | 34118 | |
| Version | PR #25964 | |
| Bundle ID | org.wordpress.alpha | |
| Commit | e5d2397 | |
| Installation URL | 1figo67q5i8og |
Contributor
|
| App Name | Jetpack | |
| Configuration | Release-Alpha | |
| Build Number | 34118 | |
| Version | PR #25964 | |
| Bundle ID | com.jetpack.alpha | |
| Commit | e5d2397 | |
| Installation URL | 03tqabs0sua30 |
The two sibling failure paths in createReply already DDLogError; the nil-reply guard was the one silent path, leaving a rare local Core Data materialization failure indistinguishable from a network error in the logs.
This was referenced Sep 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
createReplyForComment's completion is annotated non-null, but its implementation can hand backnil— the reply is re-resolved on the main context viaexistingObjectWithID:error:, which returnsnilwhen the just-created optimistic comment can't be resolved again (a rare Core Data race)._Nullableso the type tells the truth, and guard the single caller so anilreply reports a clean failure instead of silently forwardingniltouploadComment.Root Cause
The completion resolves its value via
[self.coreDataStack.mainContext existingObjectWithID:replyID error:nil](CommentService.m:119), which returnsnilif the optimistic comment can't be re-resolved. The header (CommentService.h) sits insideNS_ASSUME_NONNULL_BEGIN, so the unannotatedComment *has imported into Swift as a non-optionalCommentsince the method was refactored into this shape in 2023 (63aa79dedd). Callers therefore couldn't guard the value even though the implementation can producenil.It's been harmless so far because the one caller only forwards
replytouploadComment(reply, …)— Objective-C, where messagingnilis a safe no-op. It has never been dereferenced in Swift.Fix
_Nullableon the callback inCommentService.h/.m, so it imports into Swift asComment?.guard let replyinCommentDetailViewController.createReply— anilreply resumes the continuation withURLError(.unknown)(the error the method's existing failure branch already uses) rather than messagingnil.uploadCommenttakes a non-nullComment, so the guard is required oncereplyis optional.withUnsafeThrowingContinuation'sTfrom being inferred from the nestedcontinuation.resume(). Pin it explicitly withUnsafeContinuation<Void, Error>— the same idiom already used inBlogService+SettingsandJetpackConnectionViewModel.Test plan
guard letonly compiles once_Nullablemakesreplyimport asComment?, so a green build confirms the annotation and the guard are consistent.There are no existing tests over
createReplyForComment(a thin Core Data + callback wrapper), so this contract fix doesn't add one.Related
Splits a correctness fix out of #25945, which adds the first Swift dereference of this callback (
TaggedManagedObjectID(reply)) and would otherwise turn this latent inaccuracy into a reachable crash. Landing this first lets #25945 rebase onto an already-_Nullablecallback with the guard in place.