Skip to content

Commit

Permalink
Prevent reusing the socket after receiving a server_disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryWilber committed Nov 22, 2019
1 parent 5b2297e commit 99bc602
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const socketReferenceBufferTime = 2000;

interface ISocketReference {
socket: SocketIOClient.Socket | undefined;
socketClosing?: boolean;
references: number;
delayDeleteTimeout?: NodeJS.Timeout;
delayDeleteTimeoutSetTime?: number;
Expand Down Expand Up @@ -144,17 +145,18 @@ export class OdspDocumentDeltaConnection extends DocumentDeltaConnection impleme
timeout: timeoutMs,
});

socketReference = {
socket,
references: 1,
};

socket.on("server_disconnect", (socketError: IOdspSocketError) => {
// Raise it as disconnect.
// That produces cleaner telemetry (no errors) and keeps protocol simpler (and not driver-specific).
socketReference!.socketClosing = true;
socket.emit("disconnect", errorObjectFromOdspError(socketError));
});

socketReference = {
socket,
references: 1,
};

OdspDocumentDeltaConnection.socketIoSockets.set(key, socketReference);
debug(`Created new socketio reference for ${key}`);
}
Expand All @@ -180,7 +182,7 @@ export class OdspDocumentDeltaConnection extends DocumentDeltaConnection impleme

debug(`Removed socketio reference for ${key}. Remaining references: ${socketReference.references}.`);

if (isFatalError || (socketReference.socket && !socketReference.socket.connected)) {
if (isFatalError || socketReference.socketClosing || (socketReference.socket && !socketReference.socket.connected)) {
// delete the reference if a fatal error occurred or if the socket is not connected
if (socketReference.socket) {
socketReference.socket.disconnect();
Expand Down Expand Up @@ -214,9 +216,9 @@ export class OdspDocumentDeltaConnection extends DocumentDeltaConnection impleme
* @param socketReferenceKey - socket reference key
*/
constructor(
socket: SocketIOClient.Socket,
documentId: string,
private socketReferenceKey: string | undefined) {
socket: SocketIOClient.Socket,
documentId: string,
private socketReferenceKey: string | undefined) {
super(socket, documentId);
}

Expand All @@ -228,7 +230,7 @@ export class OdspDocumentDeltaConnection extends DocumentDeltaConnection impleme
throw new Error("Invalid socket reference key");
}

OdspDocumentDeltaConnection.removeSocketIoReference(this.socketReferenceKey);
OdspDocumentDeltaConnection.removeSocketIoReference(this.socketReferenceKey, socketProtocolError);
this.socketReferenceKey = undefined;

this.emit("disconnect", "client closing connection");
Expand Down

0 comments on commit 99bc602

Please sign in to comment.