Skip to content

wait for Y.js sync before disconnecting collaborative sessions#3675

Open
mattConnHarbour wants to merge 1 commit into
mainfrom
matthew/sd-3396-fix-wait-for-yjs-sync-before-disconnecting-collaborative
Open

wait for Y.js sync before disconnecting collaborative sessions#3675
mattConnHarbour wants to merge 1 commit into
mainfrom
matthew/sd-3396-fix-wait-for-yjs-sync-before-disconnecting-collaborative

Conversation

@mattConnHarbour
Copy link
Copy Markdown
Contributor

@mattConnHarbour mattConnHarbour commented Jun 7, 2026

Summary

  • Wait for Y.js sync before disconnecting: When closing a collaborative document, the SDK was immediately disconnecting without waiting for pending updates to flush to the server. This caused edits to be lost if close() was called too quickly after editing. The fix adds waitForFinalFlush() which waits for the websocket buffer to drain before disconnecting.

  • Allow close() without --discard for collab-only sessions: Previously, close() on a collab-only session (no source file) threw "unsaved changes" errors, forcing users to use close({ discard: true }). Since collab sessions persist changes to the server (not a local file), the "dirty" flag doesn't apply. Now close() works without --discard for collab-only sessions.

@mattConnHarbour mattConnHarbour requested a review from a team as a code owner June 7, 2026 20:36
@linear-code
Copy link
Copy Markdown

linear-code Bot commented Jun 7, 2026

SD-3396

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e15df4b379

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@@ -40,7 +40,7 @@ export type EditorWithDoc = Editor & {
export interface OpenedDocument {
editor: EditorWithDoc;
meta: DocumentSourceMeta;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Await async document disposal at call sites

Changing OpenedDocument.dispose() to optionally return a promise makes collaborative one-shot/session cleanup asynchronous, but most callers still invoke it without await (for example mutation-orchestrator.ts and save.ts finally blocks). In those non-host collaborative commands, openCollaborativeDocument.dispose() now starts runtime.waitForSync() and returns immediately to the CLI, so the process can finish before the Y.js sync completes and before the provider is disconnected; the intended “wait before disconnecting” behavior only works in the session-pool path that was updated.

Useful? React with 👍 / 👎.

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

When closing a collaborative document, the SDK was immediately
disconnecting the Y.js provider without waiting for pending updates
to be flushed to the server. This caused a race condition where the
last edits could be lost if close() was called too quickly after
editing.

Additionally, close() without --discard was throwing "unsaved changes"
errors in collab-only sessions (no source file). Since collab sessions
persist changes to the server, not a local file, the "dirty" flag
doesn't apply the same way.

Fixes:
- waitForFinalFlush() ensures Y.js updates reach the server before disconnect
- Allow close() without --discard for collab-only sessions

Fixes: SD-3396
Related: IT-1142

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@mattConnHarbour mattConnHarbour force-pushed the matthew/sd-3396-fix-wait-for-yjs-sync-before-disconnecting-collaborative branch from e15df4b to c47ab1b Compare June 7, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants