lexical: Bug: undo causes unexpected paragraph deletion in collaborative editor

When using CollaborationPlugin and two users are editing the same paragraph, an undo from one user can cause Lexical and the Yjs doc to get out of sync, leading to the whole paragraph getting cleared on page refresh.

Lexical version: 0.17.1

In this screen recording, observe the behaviour after User 1 (left) clicks the undo button: it unexpectedly deletes a character from User 2’s word, then after a refresh of the page the whole paragraph is cleared:

https://github.com/user-attachments/assets/3f631b41-99a2-4cd3-8402-2a9334e06877

When we replicate the issue in our own application we have detection code that shows Lexical editor state and the Yjs document are getting out of sync immediately after the user presses undo. Lexical thinks the second paragraph has the text node with User 2’s text, while the Yjs doc thinks the second paragraph node is empty.

Steps To Reproduce

  1. Open two windows to https://playground.lexical.dev/?isCollab=true
  2. On the left, put the cursor at the start of an empty second paragraph. (The issue still presents in the first paragraph, but this demonstrates it’s not unique to the start of the document.)
  3. On the left, type: This is a, <backspace>, a test.
  4. On the right, type: Word
  5. On the left, click the “Undo” button until all of This is a test. is removed. Sometimes this happens after one undo, sometimes multiple undo’s.
  6. Refresh the page

The current behavior

The main unexpected behaviour is that after the undo operation, a refresh of the page causes the whole paragraph to be cleared, including the text from User 2.

In the specific case captured in the screen recording, it was also unexpected that User 1’s undo operation deleted the W of Word from User 2.

The expected behavior

User 1’s undo operation should undo only the text they typed, and a refresh of the page after the undo should not lead to the paragraph content getting cleared.

Impact of fix

This bug can cause loss/corruption of data for all users of the Lexical collaboration plugin.

Note that although the data corruption behaviour is usually the deleted paragraph as shown in the screen recording, in some cases we were able to reproduce the type of duplicated and garbled text seen in https://github.com/facebook/lexical/pull/6523 and https://github.com/facebook/lexical/pull/6374. We can’t reliably reproduce this now, but it required both User 1 and User 2 performing edits on the affected paragraph right after pressing undo but before refreshing the page. We know at that point Lexical and Yjs are out of sync, so some combination of edits leads to further corruption.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 23 (9 by maintainers)

Most upvoted comments

@james-atticus After cloning the repo and running a clean version of the playground (version 0.30.0) in dev, if I set ?isCollab=true, just about any update to the editor state triggers:

Invalid access: Add Yjs type to a document before reading data.

I believe the editor/state are updating correctly (in my own project I am able to persist the editor state normally through a websocket, though I haven’t been able to resolve the warning)