[lexical] Feature: Detect infinite recursion in update listeners#8542
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
May 22, 2026
| }, | ||
| {discrete: true}, | ||
| ); | ||
| expect(errorListener).toHaveBeenCalledTimes(1); |
Collaborator
There was a problem hiding this comment.
The update listener is not doing a discrete update so there should probably be a similar drain here to ensure it's not still going?
Contributor
Author
There was a problem hiding this comment.
Pushed the drain — for (let i = 0; i < 10; i++) await Promise.resolve(); after the recovery update.
7954789 to
d4e2552
Compare
Merged
This was referenced Jun 2, 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.
Description
editor.registerUpdateListeneris documented as something to "watch out for infinite loops" with, but there is no runtime detection. When a listener callseditor.update()and the resulting state change is dirty enough to fire the listener again, the chain runs synchronously through$triggerEnqueuedUpdates→$beginUpdate→ commit → listener →_updates.push→$triggerEnqueuedUpdates... until the browser hangs. The existinginfiniteTransformCountcounter resets per$beginUpdate, so it can't catch a cascade that crosses update lifecycles.Fix
Add a per-editor
_cascadeCountcounter that increments in$triggerEnqueuedUpdateswhen the queue is non-empty and resets when the chain naturally drains or the editor is reset. Once the counter exceeds the threshold, surface a cascade-specificinvariantthrougheditor._onErrorand clear the pending update queue so the chain is cut even if the user'sonErrorswallows the throw. The counter is editor-instance state rather than module-level because a listener on editor A can synchronously calleditor.update()on editor B; sharing the counter would let A's cascade exhaust B's budget.Closes #3694
Design notes
try/catchwraps only the cascade-detectioninvariant, not the surrounding$beginUpdatecall, so it doesn't intercept the deliberate re-throw from$commitPendingUpdates's reconciler-recovery path.infiniteTransformCount(99).Test plan
pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx— 79 tests pass (new "Detects infinite recursivity on update listeners" alongside the existing transform-recursivity test)pnpm vitest run --project unit— 2585 tests pass, no NestedEditor regressionpnpm tsc --noEmit -p tsconfig.jsoncleanpnpm flowcleannpx prettier --checkclean on changed files