From 170b739f147cb6c92b423729b877e242e376927d Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 5 Jan 2021 10:44:38 +0100 Subject: [PATCH] fix: properly clear timeout on connection failure Related: https://github.com/socketio/socket.io/issues/3720 --- lib/client.ts | 14 ++++++++++---- test/socket.io.ts | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index a4c34b702e..70112e79d5 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -113,15 +113,16 @@ export class Client { * @private */ private doConnect(name: string, auth: object) { - if (this.connectTimeout) { - clearTimeout(this.connectTimeout); - this.connectTimeout = undefined; - } const nsp = this.server.of(name); const socket = nsp._add(this, auth, () => { this.sockets.set(socket.id, socket); this.nsps.set(nsp.name, socket); + + if (this.connectTimeout) { + clearTimeout(this.connectTimeout); + this.connectTimeout = undefined; + } }); } @@ -277,5 +278,10 @@ export class Client { this.conn.removeListener("close", this.onclose); // @ts-ignore this.decoder.removeListener("decoded", this.ondecoded); + + if (this.connectTimeout) { + clearTimeout(this.connectTimeout); + this.connectTimeout = undefined; + } } } diff --git a/test/socket.io.ts b/test/socket.io.ts index 0df28c636f..163fb47c4e 100644 --- a/test/socket.io.ts +++ b/test/socket.io.ts @@ -774,6 +774,29 @@ describe("socket.io", () => { }); }); + it("should close a client without namespace", (done) => { + const srv = createServer(); + const sio = new Server(srv, { + connectTimeout: 100, + }); + + sio.use((_, next) => { + next(new Error("nope")); + }); + + srv.listen(() => { + const socket = client(srv); + + const success = () => { + socket.close(); + sio.close(); + done(); + }; + + socket.on("disconnect", success); + }); + }); + describe("dynamic namespaces", () => { it("should allow connections to dynamic namespaces with a regex", (done) => { const srv = createServer();