Repository navigation
Description
Initial Checklist
- I understand this is a bug report and questions should be posted in the Community Discussions
- I searched issues and couldn’t find anything (or linked relevant results below)
Bug Description
When a file in a yjs session is changed outside the room, every clean writer that has it open receives the SSE event. Each one waits the same EXTERNAL_UPDATE_ETAG_GRACE_MS, fetches the file and calls applyExternalUpdate, which claims the stale recovery via _oc_meta.recoveryClientId. So all clean writers claim at nearly the same moment.
The only thing that elects one of them is the 150 ms wait in runStaleRecovery (useYjsSession.ts). Afterwards, each client checks whether it is still recoveryClientId. If a claim needs longer than that to reach the other peer through the yjs server, both clients see only their own claim, both run reset + hydrate, and the room ends up with the new body twice. The same happens when one client already saw the other claim but wins the last-write-wins resolution of the recoveryClientId key.
This needs a relay time of roughly 150 ms between two peers (mobile, VPN, a distant server). Before SSE, this path only ran for a single late joiner, so concurrent claims were rare. Now it is the normal path for every external write while two or more writers have the file open.
Related: #3132 (same recovery election, different failure).
Possible fix: let the yjs server decide who recovers, like it already decides who seeds. Generalize services/yjs/src/lib/seedGrant.ts to a per-room grant keyed e.g. seed / recover:<etag>. The stale flag still goes up before the grant request, so dirty peers leave the room before the rewrite arrives. This also removes recoveryClientId, isClaimantPresent and the 150 ms wait.
Reproduction Steps
The steps simulate the relay latency by delaying outgoing WebSocket messages in the browser. Without it, the same happens on real networks where a message from peer A via the yjs server to peer B takes about 150 ms or more.
- Start the dev stack (
docker-compose up -d), so the yjs server and SSE are available. - Open the same markdown file in the text editor in two tabs (A and B), as users with write access who are members of the space (e.g.
admintwice, in their personal space). Don't type anything, both tabs must have no unsaved changes. - In both tabs, run this in the DevTools console to delay every outgoing WebSocket message by 500 ms:
const send = WebSocket.prototype.send WebSocket.prototype.send = function (data) { setTimeout(() => send.call(this, data), 500) }
- Change the file outside the editor, e.g. in a third tab via the Files app (upload a modified file with the same name and choose "Replace"), or via WebDAV:
curl -k -u admin:admin -T modified.md https://host.docker.internal:9200/remote.php/webdav/<file>.md
- Wait about 3 seconds (2 s grace period plus the fetch).
Expected Outcome
Exactly one client recovers the room. Both editors show the new file content once and the "File updated" message.
Actual Outcome
Both clients recover the room. Both editors show the new file content twice, one copy after the other. The next save (manual or autosave) writes the duplicated content to the file.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity