Skip to content

[lexical] Bug Fix: use devInvariant for update recursion guard to avoid reporting a recovered condition as an uncaught error - #8631

Merged
potatowagon merged 1 commit into
mainfrom
navi/fix/recursion-guard-dev-invariant
Jun 4, 2026
Merged

potatowagon merged 1 commit into
mainfrom
navi/fix/recursion-guard-dev-invariant

Conversation

@potatowagon

@potatowagon potatowagon commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

#8542 added a guard in $triggerEnqueuedUpdates that detects when update listeners endlessly enqueue more updates (infinite recursion). The guard breaks the cascade by clearing the update queue, then signaled the condition by throwing an invariant inside a try/catch and routing the error to editor._onError.

The problem: editor._onError defaults to console.error (and in many products is wired to error reporting). Because the recursion guard is a recoverable, self-healing internal condition — the cascade is already broken before it signals — routing it through _onError reports an already-handled, non-fatal event as an uncaught error.

This mirrors #8617, which downgraded the $assumeActiveEditor precondition from invariant to devInvariant for the same reason: throw loudly in development, warn quietly in production.

What changed

  • packages/lexical/src/LexicalUpdates.ts: the recursion guard now signals via devInvariant instead of invariant + try/catch + editor._onError.
    • Dev: throws (loud, visible during development) — same severity as before.
    • Prod: console.warn only — no longer surfaces as an uncaught error via _onError.
    • The cascade is still broken (queue cleared, _cascadeCount reset) before the signal, so recursion is stopped in both builds. The namespace is preserved in the message (%s interpolation) for attribution.
  • packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx: updated the "Detects infinite recursivity on update listeners" test. Since devInvariant no longer routes through _onError, the test now captures the unhandled error surfaced from the scheduled microtask, asserts the message + namespace, confirms _onError is not invoked, and that the editor recovers after the cascade is cut.

Reproduction

We attempted to construct a minimal repro of the underlying infinite-update loop (the condition #8542 guards against) but were not able to consistently reproduce it. Notes on why it is hard to trigger:

  • Recoverable, near-invisible symptom. The guard caps the cascade, drops the batch, and the editor recovers. De-duplicated error reporting logs it about once per page load, so it presents as a single quiet log line plus a brief stutter — not a crash or a flood.
  • Unhelpful stack. The signal throws in a deferred microtask, so the stack is all minified core frames (the detector) with no application frames pointing at the offending transform.
  • Aggregation hides specifics. Errors are grouped by editor namespace, lumping multiple surfaces and content shapes into one bucket, and user content in the payload is privacy-stripped, so the literal triggering string isn't recoverable.

Given the difficulty of a deterministic repro, this PR focuses on the severity downgrade so that this recoverable condition no longer reports as an uncaught error in production, while remaining loud in development.

Test plan

pnpm test-unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx — all 79 tests pass, including the updated recursion test.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 4, 2026
@vercel

vercel Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Jun 4, 2026 8:30am
lexical-playground Ready Ready Preview, Comment Jun 4, 2026 8:30am

Request Review

@potatowagon
potatowagon force-pushed the navi/fix/recursion-guard-dev-invariant branch from 8dbdcc7 to 1e97e42 Compare June 4, 2026 08:29
@potatowagon potatowagon changed the title fix(lexical): use devInvariant for update recursion guard to avoid polluting prod fatals fix(lexical): use devInvariant for update recursion guard to avoid reporting a recovered condition as an uncaught error Jun 4, 2026
@potatowagon potatowagon changed the title fix(lexical): use devInvariant for update recursion guard to avoid reporting a recovered condition as an uncaught error [lexical] Bug Fix: use devInvariant for update recursion guard to avoid reporting a recovered condition as an uncaught error Jun 4, 2026
@etrepum

etrepum commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Why not run the test twice, once with devInvariant mocked to throw and once mocked to warn?

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the test could be better, we should know what happens when devInvariant doesn't throw here. The tests only check when it does. Fine to merge in the meantime though.

@potatowagon

Copy link
Copy Markdown
Contributor Author

I think the test could be better, we should know what happens when devInvariant doesn't throw here. The tests only check when it does. Fine to merge in the meantime though.

thanks @etrepum ! ill fix it in a followup PR

@potatowagon
potatowagon added this pull request to the merge queue Jun 4, 2026
Merged via the queue into main with commit 4083856 Jun 4, 2026
80 of 81 checks passed
@etrepum etrepum mentioned this pull request Jun 25, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 1e97e42d Deployed Jun 4, 2026 by vercel[bot]
Preview – lexical-playground — 1e97e42d Deployed Jun 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants