From f6c5fb4c897a01f0a77eaf12fe1aac03720a9d5a Mon Sep 17 00:00:00 2001 From: Cestercian Date: Mon, 23 Nov 2026 07:40:19 +0000 Subject: [PATCH] fix(sio): ignore a duplicate CONNECT packet while the namespace connection is pending --- packages/socket.io/lib/client.ts | 17 ++++++++++ packages/socket.io/lib/namespace.ts | 2 ++ packages/socket.io/test/namespaces.ts | 47 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/packages/socket.io/lib/client.ts b/packages/socket.io/lib/client.ts index 08f4b1ec46..44822ae424 100644 --- a/packages/socket.io/lib/client.ts +++ b/packages/socket.io/lib/client.ts @@ -56,6 +56,7 @@ export class Client< SocketId, Socket > = new Map(); + private readonly connectingNsps = new Set(); private nsps: Map< string, Socket @@ -126,6 +127,7 @@ export class Client< * @private */ private connect(name: string, auth: Record = {}): void { + this.connectingNsps.add(name); if (this.server._nsps.has(name)) { debug("connecting to namespace %s", name); return this.doConnect(name, auth); @@ -143,6 +145,7 @@ export class Client< this.doConnect(name, auth); } else { debug("creation of namespace %s was denied", name); + this.connectingNsps.delete(name); this._packet({ type: PacketType.CONNECT_ERROR, nsp: name, @@ -167,6 +170,7 @@ export class Client< const nsp = this.server.of(name); nsp._add(this, auth, (socket) => { + this.connectingNsps.delete(name); this.sockets.set(socket.id, socket); this.nsps.set(nsp.name, socket); @@ -177,6 +181,15 @@ export class Client< }); } + /** + * Called when a pending connection to a namespace fails. + * + * @private + */ + _onConnectFailed(name: string): void { + this.connectingNsps.delete(name); + } + /** * Disconnects from all namespaces and closes transport. * @@ -281,6 +294,10 @@ export class Client< const socket = this.nsps.get(namespace); if (!socket && packet.type === PacketType.CONNECT) { + if (this.connectingNsps.has(namespace)) { + debug("already connecting to namespace %s, ignoring", namespace); + return; + } this.connect(namespace, authPayload); } else if ( socket && diff --git a/packages/socket.io/lib/namespace.ts b/packages/socket.io/lib/namespace.ts index 72df7301e3..adbcaf41a2 100644 --- a/packages/socket.io/lib/namespace.ts +++ b/packages/socket.io/lib/namespace.ts @@ -357,12 +357,14 @@ export class Namespace< process.nextTick(() => { if ("open" !== client.conn.readyState) { debug("next called after client was closed - ignoring socket"); + client._onConnectFailed(this.name); socket._cleanup(); return; } if (err) { debug("middleware error, sending CONNECT_ERROR packet to the client"); + client._onConnectFailed(this.name); socket._cleanup(); if (client.conn.protocol === 3) { return socket._error(err.data || err.message); diff --git a/packages/socket.io/test/namespaces.ts b/packages/socket.io/test/namespaces.ts index 9bb557319a..6046dce6ac 100644 --- a/packages/socket.io/test/namespaces.ts +++ b/packages/socket.io/test/namespaces.ts @@ -59,6 +59,53 @@ describe("namespaces", () => { }); }); + it("should ignore a duplicate CONNECT packet while the first one is pending", (done) => { + const io = new Server(0); + const clientSocket = createClient(io, "/", { transports: ["websocket"] }); + let count = 0; + + io.use((socket, next) => { + // keep the first CONNECT pending while the second one is decoded + setTimeout(next, 50); + }); + + io.on("connection", () => { + count++; + }); + + clientSocket.io.engine.on("open", () => { + clientSocket.io.engine.write("0"); + }); + + clientSocket.on("connect", () => { + setTimeout(() => { + expect(count).to.eql(1); + expect(io.of("/").sockets.size).to.eql(1); + success(done, io, clientSocket); + }, 100); + }); + }); + + it("should allow a new CONNECT after a middleware error", (done) => { + const io = new Server(0); + const clientSocket = createClient(io, "/", { transports: ["websocket"] }); + let attempts = 0; + + io.use((socket, next) => { + attempts++; + next(attempts === 1 ? new Error("nope") : undefined); + }); + + clientSocket.on("connect_error", () => { + clientSocket.connect(); + }); + + clientSocket.on("connect", () => { + expect(attempts).to.eql(2); + success(done, io, clientSocket); + }); + }); + it("should fire a `connect` event", (done) => { const io = new Server(0); const clientSocket = createClient(io);