fix(super-editor): null-safe getTrackChanges to avoid init-race crash (SD-2641)#3253
Open
tupizz wants to merge 1 commit into
Open
Conversation
… (SD-2641) createCommentForTrackChanges is scheduled from processLoadedDocxComments via setTimeout(0). That only defers to the next tick; it does not wait for the editor's PM state to be initialized. When the bootstrap fires before editor.state is attached, getTrackChanges(editor.state) becomes getTrackChanges(undefined) and crashes reading state.doc. The same race affects all four call sites of getTrackChanges in comments-store.js (lines 294, 1143, 1453, 1545), so harden the helper once rather than guarding each caller. A pure helper returning [] when there is no state to inspect is the semantically correct null behaviour. Adds focused unit tests covering undefined / null / missing-doc inputs.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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
createCommentForTrackChangesis scheduled fromprocessLoadedDocxCommentsviasetTimeout(0). That only defers to the next tick — it does not wait for the editor's PM state to be attached. When the bootstrap fires beforeeditor.stateexists,getTrackChanges(editor.state)becomesgetTrackChanges(undefined)and crashes readingstate.doc(the stack trace's"reading 'doc'").The same race affects all four call sites of
getTrackChangesincomments-store.js(lines 294, 1143, 1453, 1545), so I harden the helper once rather than guarding each caller. A pure helper returning[]when there's no state to inspect is the semantically correct null behaviour — "no state → no tracked changes."Linear
Why fix at the helper, not the caller
comments-store.jscallsitesHelpers in JS libraries idiomatically null-check their input. Returning
[]from a "find tracked changes" function on a missing doc is the only sensible answer — not throwing.Test plan
undefined,null,{},{ doc: undefined }. All reproduce the bug's exact error chain (Cannot read properties of undefined (reading 'doc')andInvalid "node" parameterinfindChildren).super-editorsuite: 12,723 passed, 13 skipped, 0 failed (+4 from baseline).Out of scope
setTimeout(0)bootstrap to wait on aeditorReadypromise. That's a deeper change (touches the editor lifecycle contract); this PR closes the user-visible crash without changing the timing model.comments-store.test.js; not duplicating that coverage here.