Skip to content

fix(data-connect): bind WebSocket handlers to their own socket - #10699

Open
debojyoti10CC wants to merge 1 commit into
firebase:mainfrom
debojyoti10CC:fix/dataconnect-ws-handler-binding
Open

debojyoti10CC wants to merge 1 commit into
firebase:mainfrom
debojyoti10CC:fix/dataconnect-ws-handler-binding

Conversation

@debojyoti10CC

Copy link
Copy Markdown

Fixes #10696

What was wrong

In WebSocketTransport.ensureConnection, the onopen/onerror/onmessage/onclose handlers act on this state rather than on the socket they were registered for. closeConnection clears this.connection immediately, but the browser fires that socket's close a bit later. If a send opens socket B in that window, socket A's late onclose runs handleWebsocketDisconnect, which clears B and starts a reconnect, so B is orphaned and the cycle repeats (this is easy to hit after the 50-minute stream-lifetime frame).

Fix

Each handler now checks whether another socket has replaced its own (this.connection is set and is not ws). If so, the event is ignored, and for onopen/onerror/onclose the superseded socket's pending connection attempt is rejected. Events for the current socket, or when no socket is tracked (the existing "closed locally, reconnect on close" path), behave as before. I kept this to handler binding only; the other points in the issue (re-sending subscriptions on every new connection, recognising the lifetime frame) are not changed here.

Testing

Added three tests in websocketTransport.test.ts using the existing MockWebSocket: late onclose from a replaced socket, late onerror/onmessage, and a pending attempt rejected when its socket is superseded.

I could not run vitest: the monorepo yarn install failed on this Windows machine (EBUSY on cache extraction, then a hang), so the tests are unrun. I checked formatting with prettier and reviewed the logic by hand. Please rely on CI for the test run.

Changeset added for @firebase/data-connect (patch).

@debojyoti10CC
debojyoti10CC requested review from a team as code owners November 30, 2026 01:16
@google-cla

google-cla Bot commented Nov 30, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@changeset-bot

changeset-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 39f9e47

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@firebase/data-connect Patch
firebase Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue where late-arriving events from a superseded WebSocket connection could interfere with a newer connection, potentially causing orphaned sockets and reconnect loops. The fix introduces checks to ignore events (such as open, error, message, and close) if the socket has been superseded by a newer connection, and includes comprehensive unit tests to verify this behavior. The review feedback suggests minor code cleanups: removing an unused event parameter in the onerror handler and removing an unnecessary async keyword from a .catch callback.

resolve();
};

ws.onerror = event => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The event parameter in the onerror handler is unused. Removing it keeps the code clean and avoids potential compiler or linter warnings about unused parameters.

Suggested change
ws.onerror = event => {
ws.onerror = () => {

if (isSuperseded()) {
return;
}
this.handleWebSocketMessage(ev).catch(async reason => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The async keyword in the .catch() handler is unnecessary because there are no await expressions inside the callback. Removing it avoids the overhead of creating an unused promise and keeps the code cleaner.

Suggested change
this.handleWebSocketMessage(ev).catch(async reason => {
this.handleWebSocketMessage(ev).catch(reason => {

This branch has not been deployed

No deployments
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.

DataConnect: WebSocket handlers are not bound to their socket, so the 50-minute stream lifetime close orphans sockets and loops

1 participant