Skip to content

🐛 Broadcast a leave before presence re-created with its ID - #772

Closed
alecgibson wants to merge 1 commit into
supersede-presence-on-publishfrom
presence-leave-order
Closed

alecgibson wants to merge 1 commit into
supersede-presence-on-publishfrom
presence-leave-order

Conversation

@alecgibson

@alecgibson alecgibson commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #771

At the moment, a presence re-created with the ID of one that's leaving can be transformed and published before the leave:

  1. Destroy a presence, and hold its leave's snapshot read in the database
  2. Re-create it with the same ID, at the doc's version
  3. Release the leave: subscribers drop the re-created presence, since its pv restarted, and then clear it with the leave

This change has presence wait, once through middleware, until each leave of its ID that arrived before it has been handed to pubsub, superseded or rejected. It checks again after each one, since a later leave can be rejected while an earlier one is still transforming. Presence retracted by an unsubscribe or a disconnect neither waits nor is waited on, since it's either dropped or goes out as a leave, and the client's stored presence gets a leave of its own.

Waiters are released once the leave's publish() is called, not when it calls back, so pubsub adapters must deliver an instance's messages in call order, as both the memory and Redis adapters do. That way an adapter that never calls back can't hold up the ID.

🤖 Generated with Claude Code

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

@coveralls

coveralls commented Nov 5, 2026 •

Copy link
Copy Markdown

Coverage Status

Coverage is 98.094% — presence-leave-order into supersede-presence-on-publish. No base build found for supersede-presence-on-publish.

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 queue-handling bug can acknowledge a valid presence update without publishing it when a newer queued update fails transformation, leaving subscribers without replacement presence.

1 open issue
What changed in this PR

This PR addresses part of #771 by ordering server-side presence broadcasts so a re-created presence waits for an earlier leave with the same ID.

Changes:

  • Queue same-ID broadcasts until the preceding leave is handed to pubsub or rejected, releasing waiters without recursion.
  • Shallow-copy disconnect leaves to avoid mutating pending presence updates.
  • Add regression tests for ordering, failures, disconnects, and long leave queues.
File Description
test/​client/​presence/​doc-presence.js Adds helpers and regression cases for queued presence broadcasts.
lib/​agent.js Tracks in-flight leaves, orders broadcasts, and copies disconnect leaves.
docs/​middleware/​actions.md Documents shallow-copy behavior for disconnect and unsubscribe leaves.

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.

🟢 Approval recommended

No active review issues are recorded for the presence-ordering fix.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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

Outstanding issues retain queued update closures, payloads, and callbacks indefinitely when a publish callback never fires and leave update acknowledgements potentially blocked after the preceding broadcast is retracted.

2 open issues

Review effort: Balanced

Comment thread lib/agent.js Resolved
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 retracted same-ID update can remain blocked behind a held server-generated leave after a second unsubscribe, preventing its submit callback from completing and requiring a cancellation fix with regression coverage.

1 open issue
2 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 race remains because middleware-generated leaves retain a stale pending flag, allowing a delayed leave to clear a newly recreated presence on subscribers.

1 open issue
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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

An active issue in lib/agent.js can leave same-ID re-creation broadcasts and acknowledgements blocked indefinitely because superseded leave broadcasts do not release existing waiters.

1 open issue

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

Canceled updates remain in the blocking leave’s waiters array after completion, causing unbounded retention during repeated subscribe/submit/unsubscribe cycles if the leave stalls.

1 open issue
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

Disconnect cleanup misses queued updates for channels removed by unsubscribe, potentially retaining their payloads and callbacks indefinitely; retraction must cover pending broadcasts and include a regression test.

1 open issue
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

Publishing a newer same-ID update can trigger quadratic scans of pending broadcasts, potentially blocking the server’s event loop during bursts.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread lib/agent.js Resolved
Refs #771

At the moment, a presence re-created with the ID of one that's leaving
can be transformed and published before the leave:

 1. Destroy a presence, and hold its leave's snapshot read in the
    database
 2. Re-create it with the same ID, at the doc's version
 3. Release the leave: subscribers drop the re-created presence, since
    its `pv` restarted, and then clear it with the leave

This change has presence wait, once through middleware, until each
leave of its ID that arrived before it has been handed to pubsub,
superseded or rejected. It checks again after each one, since a later
leave can be rejected while an earlier one is still transforming.
Presence retracted by an unsubscribe or a disconnect neither waits nor
is waited on, since it's either dropped or goes out as a leave, and the
client's stored presence gets a leave of its own.

Waiters are released once the leave's `publish()` is called, not when
it calls back, so pubsub adapters must deliver an instance's messages
in call order, as both the [memory][1] and [Redis][2] adapters do. That
way an adapter that never calls back can't hold up the ID.

[1]: https://github.com/share/sharedb/blob/76c01034f653d68d42b257803f0e0eb6e66aa1b8/lib/pubsub/memory.js#L28-L39
[2]: https://github.com/share/sharedb-redis-pubsub/blob/4909f88651d2d91086857218ea2f94fde107b156/index.js#L79-L85

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

3 participants