From 8a29424dcce7cf597eac9ac392caf069dd4644a4 Mon Sep 17 00:00:00 2001 From: Mark Russell <39885340+mark1russell7@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:29:26 -0500 Subject: [PATCH 1/2] fix(sio-client): discard a volatile packet when the namespace is not connected A volatile packet was discarded only when the transport was not writable. During the handshake of a namespace (for example a second namespace on a connected Manager, or right after a reconnection), the transport is writable but the socket is not connected yet, so the packet was buffered and sent once connected. The documentation of the volatile flag says the event "will be dropped when this socket is not ready to send messages". --- packages/socket.io-client/lib/socket.ts | 7 +++++-- packages/socket.io-client/test/socket.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/socket.io-client/lib/socket.ts b/packages/socket.io-client/lib/socket.ts index 13c0ed97ce..4549dcf1eb 100644 --- a/packages/socket.io-client/lib/socket.ts +++ b/packages/socket.io-client/lib/socket.ts @@ -447,9 +447,12 @@ export class Socket< const isTransportWritable = this.io.engine?.transport?.writable; const isConnected = this.connected && !this.io.engine?._hasPingExpired(); - const discardPacket = this.flags.volatile && !isTransportWritable; + const discardPacket = + this.flags.volatile && !(isTransportWritable && isConnected); if (discardPacket) { - debug("discard packet as the transport is not currently writable"); + debug( + "discard packet as the socket is not connected or the transport is not currently writable", + ); } else if (isConnected) { this.notifyOutgoingListeners(packet); this.packet(packet); diff --git a/packages/socket.io-client/test/socket.ts b/packages/socket.io-client/test/socket.ts index 0149ad64b6..0e329e3b2f 100644 --- a/packages/socket.io-client/test/socket.ts +++ b/packages/socket.io-client/test/socket.ts @@ -421,6 +421,26 @@ describe("socket", () => { }); }); + it("should discard a volatile packet when the namespace is not connected yet", () => { + return wrap((done) => { + const socket = io(BASE_URL, { forceNew: true }); + + socket.on("connect", () => { + // the transport is writable, but the server has not acknowledged the CONNECT packet of this namespace yet + const fooSocket = socket.io.socket("/foo"); + + fooSocket.volatile.emit("getId", () => { + done(new Error("should not happen")); + }); + + fooSocket.emit("getId", () => { + fooSocket.disconnect(); + success(done, socket); + }); + }); + }); + }); + it("should send a volatile packet when the socket is connected and the pipe is ready", () => { return wrap((done) => { const socket = io(BASE_URL, { forceNew: true }); From d4804fdd6d81d72d952108b4f8553a7560f1b19c Mon Sep 17 00:00:00 2001 From: Mark Russell <39885340+mark1russell7@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:44:27 -0500 Subject: [PATCH 2/2] test(sio-client): emit the volatile packet once the transport is writable again Right after the CONNECT packet of the namespace, the transport is not writable, so the volatile packet was discarded even without the fix. The test now uses a namespace that delays its acknowledgement, and emits the packet once the transport is writable again. --- packages/socket.io-client/test/socket.ts | 29 ++++++++++++------- .../socket.io-client/test/support/server.ts | 12 ++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/socket.io-client/test/socket.ts b/packages/socket.io-client/test/socket.ts index 0e329e3b2f..bbdc5d2cff 100644 --- a/packages/socket.io-client/test/socket.ts +++ b/packages/socket.io-client/test/socket.ts @@ -423,20 +423,29 @@ describe("socket", () => { it("should discard a volatile packet when the namespace is not connected yet", () => { return wrap((done) => { - const socket = io(BASE_URL, { forceNew: true }); + const socket = io(BASE_URL, { + forceNew: true, + transports: ["websocket"], + }); socket.on("connect", () => { - // the transport is writable, but the server has not acknowledged the CONNECT packet of this namespace yet - const fooSocket = socket.io.socket("/foo"); + // the server delays the acknowledgement of the CONNECT packet of this namespace + const delayedSocket = socket.io.socket("/delayed"); - fooSocket.volatile.emit("getId", () => { - done(new Error("should not happen")); - }); + // wait until the transport is writable again after the CONNECT packet + setTimeout(() => { + expect(socket.io.engine.transport.writable).to.be(true); + expect(delayedSocket.connected).to.be(false); - fooSocket.emit("getId", () => { - fooSocket.disconnect(); - success(done, socket); - }); + delayedSocket.volatile.emit("getId", () => { + done(new Error("should not happen")); + }); + + delayedSocket.emit("getId", () => { + delayedSocket.disconnect(); + success(done, socket); + }); + }, 20); }); }); }); diff --git a/packages/socket.io-client/test/support/server.ts b/packages/socket.io-client/test/support/server.ts index 52a7d18f6a..6a55598816 100644 --- a/packages/socket.io-client/test/support/server.ts +++ b/packages/socket.io-client/test/support/server.ts @@ -17,6 +17,18 @@ export function createServer() { }); }); + server + .of("/delayed") + .use((socket, next) => { + // delay the acknowledgement of the CONNECT packet + setTimeout(next, 100); + }) + .on("connection", (socket) => { + socket.on("getId", (cb) => { + cb(socket.id); + }); + }); + server.of("/timeout_socket").on("connection", () => { // register namespace });