Skip to content

fix(sync): scope transport failures and session routing - #181

Merged
Antonov548 merged 4 commits into
mainfrom
fix/transport-terminal-errors
Aug 20, 2026
Merged

Antonov548 merged 4 commits into
mainfrom
fix/transport-terminal-errors

Conversation

@marcus-pousette

@marcus-pousette marcus-pousette commented Jul 11, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

When a client reconnects, the old and replacement connections may briefly overlap and reuse the same protocol IDs. SyncPeer previously keyed sessions and subscriptions by protocol ID alone.

A late response or cleanup from the old connection could therefore advance or remove state belonging to the replacement connection. Meanwhile, operations waiting on the dead connection could remain unresolved indefinitely.

Protocol IDs are connection-local, so protocol state and closure must be scoped to the transport that owns them.

This PR makes reconnecting safe when the caller supplies a fresh transport; it does not implement automatic reconnection.

Before / after

Before, transports only carried messages and responder state was addressed by ID alone:

const transport: DuplexTransport<Message> = { send, onMessage };
const session = sessions.get(protocolId);

After, every transport represents one connection lifecycle and exposes synchronous, idempotent closure:

const closeController = createTransportCloseController();
const transport: DuplexTransport<Message> = {
  send,
  onMessage,
  close(reason) {
    closeController.close(reason);
    closeUnderlyingTransport();
  },
  closeSignal: closeController.signal,
};

const session = sessions.get(transport)?.get(protocolId);

The high-level custom-transport factory now owns cleanup:

// Before
createTreecrdtWebSocketSyncFromTransport(client, transport, closeTransport, options);

// After
createTreecrdtWebSocketSyncFromTransport(client, transport, options);

This is a source-breaking change for custom transports and test doubles. There is no wire-format change.

Changes

  • Scope sessions, subscriptions, acknowledgements, and batch queues by transport and protocol ID.
  • Require close() and a read-only closeSignal; reject transport-object reuse after detach or closure.
  • Propagate browser and Node WebSocket closure through pending and live sync work.
  • Close only malformed/offending sockets while healthy connections remain usable.
  • Let the high-level sync handle close its owned transport.

closeSignal describes permanent connection closure. It does not replace AbortSignal for canceling one sync attempt.

Validation

  • Sync protocol: 42/42
  • Sync server core: 12/12
  • High-level sync: 8/8
  • TypeScript builds for affected packages and full formatting checks
  • Full CI, PostgreSQL, browser, playground e2e, and native-node checks

@marcus-pousette marcus-pousette added bug Something isn't working area:sync Sync protocol, reconciliation, transport, discovery, and sync servers area:api Public interfaces, bindings, events, and cross-backend contracts labels Jul 12, 2026
@marcus-pousette marcus-pousette changed the title fix: propagate transport termination through sync fix(sync): scope transport failures and session routing Jul 12, 2026
@marcus-pousette
marcus-pousette force-pushed the fix/transport-terminal-errors branch from d516319 to 62d828f Compare July 13, 2026 15:46
@marcus-pousette
marcus-pousette force-pushed the fix/transport-terminal-errors branch from 62d828f to 7835c30 Compare August 18, 2026 17:22
Co-Authored-By: Codex GPT-5 (unknown context) <noreply@openai.com>
@Antonov548
Antonov548 merged commit 453b2f8 into main Aug 20, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:api Public interfaces, bindings, events, and cross-backend contracts area:sync Sync protocol, reconciliation, transport, discovery, and sync servers bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants