Skip to content

fix(super-editor): null-safe getTrackChanges to avoid init-race crash (SD-2641)#3253

Open
tupizz wants to merge 1 commit into
mainfrom
tadeu/sd-2641-bug-createcommentfortrackchanges-crashes-when-editordoc-is
Open

fix(super-editor): null-safe getTrackChanges to avoid init-race crash (SD-2641)#3253
tupizz wants to merge 1 commit into
mainfrom
tadeu/sd-2641-bug-createcommentfortrackchanges-crashes-when-editordoc-is

Conversation

@tupizz
Copy link
Copy Markdown
Contributor

@tupizz tupizz commented May 12, 2026

Summary

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 attached. When the bootstrap fires before editor.state exists, getTrackChanges(editor.state) becomes getTrackChanges(undefined) and crashes reading state.doc (the stack trace's "reading 'doc'").

The same race affects all four call sites of getTrackChanges in comments-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

  • SD-2641 — SLA breached 2026-05-08. Related: SD-2635 (React-wrapper re-init).

Why fix at the helper, not the caller

Helper-level (this PR) Per-caller
Lines changed 1 + JSDoc 4 separate guards
Closes the race for all four comments-store.js callsites only one
Prevents future similar bugs from new callers
Changes the public contract of an exported helper yes — documented in JSDoc no

Helpers 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

  • Failing unit tests added at the same commit (TDD red): 4 cases — undefined, null, {}, { doc: undefined }. All reproduce the bug's exact error chain (Cannot read properties of undefined (reading 'doc') and Invalid "node" parameter in findChildren).
  • After the guard: all 4 tests pass.
  • Full super-editor suite: 12,723 passed, 13 skipped, 0 failed (+4 from baseline).
  • Manual: open a document with comments in the dev app — no console error during startup.

Out of scope

  • Refactoring the setTimeout(0) bootstrap to wait on a editorReady promise. That's a deeper change (touches the editor lifecycle contract); this PR closes the user-visible crash without changing the timing model.
  • The existing helper-positive path is covered by mocked integration tests in comments-store.test.js; not duplicating that coverage here.

… (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.
@tupizz tupizz requested a review from a team as a code owner May 12, 2026 18:55
@linear
Copy link
Copy Markdown

linear Bot commented May 12, 2026

SD-2641

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

2 participants