You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
🐛 Reject presence whose version is ahead of the doc - #770
At the moment, the server relays a doc presence whose v is ahead of the doc unchanged, which lets one client make every subscriber fetch:
Submit presence with v: 1e9 to a doc with 20 subscribers
Each subscriber calls doc.fetch(), since v > doc.version
The presence stays pending, so every later op fetches again
This change checks presence ahead of the Agent's tracked doc version against the snapshot version, and rejects it if it's still ahead. The tracker can lag the database but never lead it, so presence at or behind it needs no extra read. Doc presence must now carry an integer v. Presence without c/d skips this, but clients ignore it since #752. We deliberately don't bound presence far behind: a fetch with v: 0 already reads the whole op history.
A leave has no position to transform, so it now just takes the snapshot version instead of reading ops. That clamps a leave from the future rather than rejecting it, and stops leaves running 'op' middleware, which kept the server from clearing presence for a client that had lost read access.
The server now rejects requests that the bundled client never sends:
Doc presence without a v, or with a non-integer one, is rejected as ERR_MESSAGE_BADLY_FORMED, and so is an op that sends a non-integer v. The client always sends v: doc.version.
Doc presence whose v is ahead of the doc is rejected with ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT instead of being relayed. The client's v comes from the server, so it's never ahead, unless your database adapter reads from a replica that can lag the primary. In that case, presence from a client that has seen ops the replica hasn't is now rejected rather than relayed. null presence whose v is ahead is clamped to the doc's version and relayed instead.
'readSnapshots' middleware now also runs when a client sends null presence after already clearing another presence on the doc. A client that has since lost read access may not be able to clear that presence until it unsubscribes or disconnects.
The Agent also no longer calls Backend.prototype.transformPresenceToLatestVersion(). It's undocumented, but if you override it, the override no longer runs on presence.
Base automatically changed from
check-doc-presence-channel to
mainOctober 30, 2026 09:51
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A critical same-ID re-creation race allows an old leave to overwrite the new cached incarnation and clear its presence on subscribers, requiring corrected ordering and test coverage for both transform completion orders.
This PR addresses #735’s client fetch amplification by rejecting document presence versions ahead of the document, while handling leaves and rejected requests.
Changes:
Validate integer versions and check presence against tracked versions, operation history, and snapshots.
Restore cached presence after rejection and handle overlapping updates, leaves, and re-creations.
Add regression tests and document read-access checks for null presence.
File
Description
test/client/presence/doc-presence.js
Adds version, rejection, leave, and concurrency regression tests.
test/agent.js
Tests rejection of non-integer operation versions.
lib/backend.js
Checks presence versions and clamps future-versioned leaves.
lib/agent.js
Validates versions and manages pending presence and cache rollback.
docs/presence.md
Explains read-access checks when clearing presence.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Queued leaves cause quadratic server memory and CPU amplification, while fallback handling across re-creations can leave stale cursors or suppress valid presence updates.
Preserve presence incarnation order during fallback and cache replacement
lib/agent.js:1031
When a held leave (pv:1) is followed by a re-creation (pv:0) and a future-version update (pv:2), fallback promotion replaces the update's fallback with the old leave; rejecting the update after the re-creation broadcasts then leaves a cursor that disconnect will not clear. The cross-incarnation comparison in cacheNewerPresenceRequest (lib/agent.js:1096–1097) can also suppress a valid re-creation when rejection restores a higher-version old leave before its transform completes. Track incarnation order in fallback selection and cache replacement, and add regression tests for both cases.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical presence-handling flaws can crash the server through recursive queue draining, drop acknowledged updates during rollback, and leave stale presence visible after disconnect.
A valid update that finishes transforming while a later leave is still transforming is dropped here because the leave marked it superseded. If the leave's lookup then fails, rollback restores the already-completed update in presenceRequests without publishing it, so subscribers miss a successfully acknowledged update. Retain the completed update until the leave succeeds, or replay it on rollback, and cover this completion order in the newer-leave-fails test.
Replace stale leave fallback after presence recreation
lib/agent.js:1028
If middleware lets a re-created presence's pv: 1 update through before its pv: 0 update, the former can capture the previous incarnation's cached leave (for example, pv: 5) as its fallback. When pv: 0 succeeds and the future-versioned pv: 1 update is rejected, this comparison preserves the old leave as the fallback; disconnect then sends no leave, leaving the broadcast presence stuck on subscribers. Treat a null fallback as older than an accepted live presence regardless of its pv, since versions restart after re-creation.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A critical channel-collision flaw lets operations for another document advance the version tracker, allowing invalid presence versions to be broadcast and preserving subscriber fetch amplification.
Fixes#735
At the moment, the server relays a doc presence whose `v` is ahead of
the doc unchanged, which lets one client make every subscriber fetch:
1. Submit presence with `v: 1e9` to a doc with 20 subscribers
2. Each subscriber calls `doc.fetch()`, since `v > doc.version`
3. The presence stays pending, so every later op fetches again
This change checks presence ahead of the `Agent`'s tracked doc version
against the snapshot version, and rejects it if it's still ahead. The
tracker can lag the database but never lead it, so presence at or
behind it needs no extra read. Doc presence must now carry an integer
`v`. Presence without `c`/`d` skips this, but clients ignore it since
#752. We deliberately don't bound presence far *behind*: a `fetch` with
`v: 0` already reads the whole op history.
A leave has no position to transform, so it now just takes the
snapshot version instead of reading ops. That clamps a leave from the
future rather than rejecting it, and stops leaves running `'op'`
middleware, which kept the server from clearing presence for a client
that had lost read access.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
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
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.
Fixes #735
At the moment, the server relays a doc presence whose
vis ahead of the doc unchanged, which lets one client make every subscriber fetch:v: 1e9to a doc with 20 subscribersdoc.fetch(), sincev > doc.versionThis change checks presence ahead of the
Agent's tracked doc version against the snapshot version, and rejects it if it's still ahead. The tracker can lag the database but never lead it, so presence at or behind it needs no extra read. Doc presence must now carry an integerv. Presence withoutc/dskips this, but clients ignore it since #752. We deliberately don't bound presence far behind: afetchwithv: 0already reads the whole op history.A leave has no position to transform, so it now just takes the snapshot version instead of reading ops. That clamps a leave from the future rather than rejecting it, and stops leaves running
'op'middleware, which kept the server from clearing presence for a client that had lost read access.🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com