From d6f09f4c1a41aac661612b4280759be6d2bfe82c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sat, 6 Mar 2021 18:29:31 +0100 Subject: [PATCH] Fix close during reconnect wait period If the connect close method is called while the connection is waiting for a reconnection attempt then the attempt is cancelled but the close method then erroneously waited for a close event that would never happen. Fix by resetting the connector and rasing the close event in the close method if it's in the waiting for reconnect state. --- packages/lib/link/connectors.js | 7 +++++++ test/integration/link.js | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/lib/link/connectors.js b/packages/lib/link/connectors.js index f3881d26..0932070f 100644 --- a/packages/lib/link/connectors.js +++ b/packages/lib/link/connectors.js @@ -211,6 +211,13 @@ class WebSocketClientConnector extends WebSocketBaseConnector { if (this._reconnectId) { clearTimeout(this._reconnectId); this._reconnectId = null; + this._lastReceivedSeq = null; + this._sessionToken = null; + this._sendBuffer.length = 0; + this._state = "new"; + this._connected = false; + this.emit("close"); + return; } this.stopHeartbeat(); diff --git a/test/integration/link.js b/test/integration/link.js index 39848e16..183147f6 100644 --- a/test/integration/link.js +++ b/test/integration/link.js @@ -53,4 +53,12 @@ describe("Integration of lib/link", function() { await events.once(controlConnector, "invalidate"); await events.once(controlConnector, "connect"); }); + + it("should properly close connector if close is called during reconnect wait", async function() { + controlConnector._socket.close(1008, "Test"); + controlConnector.once("drop", () => { + controlConnector.close(); + }); + await events.once(controlConnector, "close"); + }); });