Skip to content

🐛 Reject presence whose version is ahead of the doc - #770

Merged
alecgibson merged 1 commit into
mainfrom
reject-future-presence-version
Nov 9, 2026
Merged

alecgibson merged 1 commit into
mainfrom
reject-future-presence-version

Conversation

@alecgibson

@alecgibson alecgibson commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

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

Co-Authored-By: Claude noreply@anthropic.com

@alecgibson

Copy link
Copy Markdown
Collaborator Author

Migration note

Not breaking. This can ship as a 7.x fix.

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 main October 30, 2026 09:51
@alecgibson
alecgibson requested a balanced review from Copilot October 30, 2026 09:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

1 open issue
What changed in this PR

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.
docs/​middleware/​actions.md Documents null-presence checks invoking readSnapshots.

Review effort: Balanced

Comment thread lib/agent.js Resolved
@coveralls

coveralls commented Oct 30, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 98.066% (+0.009%) from 98.057% — reject-future-presence-version into main

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity 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.

1 resolved since last review

Review effort: Balanced

Comment thread lib/agent.js Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

High severity Replay completed updates when a newer leave fails

lib/​agent.js:968

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.

High severity 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.

1 resolved since last review

Review effort: Balanced

Comment thread lib/agent.js Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread lib/backend.js Resolved
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>
@alecgibson
alecgibson force-pushed the reject-future-presence-version branch from 755955d to 62bab81 Compare November 5, 2026 20:46
@alecgibson
alecgibson requested a balanced review from Copilot November 5, 2026 20:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the presence-version validation and cleanup changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@alecgibson
alecgibson marked this pull request as ready for review November 6, 2026 17:30
@alecgibson
alecgibson merged commit 76c0103 into main Nov 9, 2026
9 checks passed
@alecgibson
alecgibson deleted the reject-future-presence-version branch November 9, 2026 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Presence version is not clamped, amplifying work on server and other clients

4 participants