Skip to content

[lexical-react] Bug Fix: prevent race condition in CollaborationPlugin during rapid mount/unmount cycles#7723

Merged
etrepum merged 1 commit intofacebook:mainfrom
tobiasandersen:fix-7722
Jul 24, 2025
Merged

[lexical-react] Bug Fix: prevent race condition in CollaborationPlugin during rapid mount/unmount cycles#7723
etrepum merged 1 commit intofacebook:mainfrom
tobiasandersen:fix-7722

Conversation

@tobiasandersen
Copy link
Copy Markdown
Contributor

Description

This fix resolves a race condition by ensuring the Yjs document reference is captured after the provider is initialized, rather than during the component's render function.

Before: The doc state was initialized with yjsDocMap.get(id) during the new component's render phase, which could capture a stale document reference because the old component instance's cleanup may not have executed yet.

After: The doc state is now set within the provider useEffect, ensuring it always references the fresh document that corresponds to the newly created provider. By doing this we guarantee that:

  • The new component's render function doesn't prematurely capture a stale document
  • The provider factory has completed and stored the current document in the map
  • Any previous component cleanup has had a chance to finish
  • The binding will be created with the correct, up-to-date document reference

This prevents the race condition where the new component's render function would capture a document reference before the old component's cleanup could run docMap.delete(id), ensuring the binding's observer is always attached to the active Yjs document that's receiving server updates.

Closes #7722

@vercel
Copy link
Copy Markdown

vercel bot commented Jul 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 24, 2025 10:57am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 24, 2025 10:57am

@facebook-github-bot facebook-github-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 Jul 24, 2025
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 24, 2025
@etrepum etrepum added this pull request to the merge queue Jul 24, 2025
Merged via the queue into facebook:main with commit f104810 Jul 24, 2025
43 checks passed
@etrepum etrepum mentioned this pull request Aug 7, 2025
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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition in CollaborationPlugin: Editor fails to sync when rapidly unmounted/remounted due to stale Yjs document references

3 participants