Skip to content

Commit 8902506

Browse files
thomasballingerConvex, Inc.
authored andcommitted
Fix WebSocket races in client (#41929)
Reports of errors from TransitionChunks highlighted new and existing race conditions. GitOrigin-RevId: d3098b790538ffef0700cac7a1a08dde5da17afe
1 parent 2454ac1 commit 8902506

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/browser/sync/web_socket_manager.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ export class WebSocketManager {
396396
}
397397

398398
if (this.transitionChunkBuffer !== null) {
399-
throw new Error(
399+
this.transitionChunkBuffer = null;
400+
this.logger.log(
400401
`Received unexpected ${serverMessage.type} while buffering TransitionChunks`,
401402
);
402403
}
@@ -552,6 +553,7 @@ export class WebSocketManager {
552553
* closed socket is not accessible or used again after this method is called
553554
*/
554555
private close(): Promise<void> {
556+
this.transitionChunkBuffer = null;
555557
switch (this.socket.state) {
556558
case "disconnected":
557559
case "terminated":
@@ -560,6 +562,10 @@ export class WebSocketManager {
560562
return Promise.resolve();
561563
case "connecting": {
562564
const ws = this.socket.ws;
565+
// Messages can still be received after close but we're not interested.
566+
ws.onmessage = (_message) => {
567+
this._logVerbose("Ignoring message received after close");
568+
};
563569
return new Promise((r) => {
564570
ws.onclose = () => {
565571
this._logVerbose("Closed after connecting");
@@ -574,6 +580,10 @@ export class WebSocketManager {
574580
case "ready": {
575581
this._logVerbose("ws.close called");
576582
const ws = this.socket.ws;
583+
// Messages can still be received after close but we're not interested.
584+
ws.onmessage = (_message) => {
585+
this._logVerbose("Ignoring message received after close");
586+
};
577587
const result: Promise<void> = new Promise((r) => {
578588
ws.onclose = () => {
579589
r();

0 commit comments

Comments
 (0)