Repository navigation
fix(data-connect): bind WebSocket handlers to their own socket - #10699
debojyoti10CC wants to merge 1 commit into
Conversation
|
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 detectedLatest commit: 39f9e47 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
There was a problem hiding this comment.
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 => { |
| if (isSuperseded()) { | ||
| return; | ||
| } | ||
| this.handleWebSocketMessage(ev).catch(async reason => { |
There was a problem hiding this comment.
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.
| this.handleWebSocketMessage(ev).catch(async reason => { | |
| this.handleWebSocketMessage(ev).catch(reason => { |
Fixes #10696
What was wrong
In
WebSocketTransport.ensureConnection, theonopen/onerror/onmessage/onclosehandlers act onthisstate rather than on the socket they were registered for.closeConnectionclearsthis.connectionimmediately, but the browser fires that socket'sclosea bit later. If a send opens socket B in that window, socket A's lateoncloserunshandleWebsocketDisconnect, 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.connectionis set and is notws). If so, the event is ignored, and foronopen/onerror/onclosethe 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.tsusing the existingMockWebSocket: lateonclosefrom a replaced socket, lateonerror/onmessage, and a pending attempt rejected when its socket is superseded.I could not run vitest: the monorepo
yarn installfailed 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).