From 2ac6572e7a63d0453b4c70fae2a438e3a64fffe1 Mon Sep 17 00:00:00 2001 From: Mark Russell <39885340+mark1russell7@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:33:16 -0500 Subject: [PATCH] fix(sio-client): accept 0 for reconnectionAttempts, reconnectionDelay and reconnectionDelayMax The constructor of the Manager and the Backoff used "||", so the value 0 in the options was replaced by the default value (Infinity, 1000 and 5000), while the setters accepted 0. With reconnectionAttempts: 0, the client retried forever, and reconnect_failed was never emitted. Related: https://github.com/socketio/socket.io-client/issues/1231 --- .../socket.io-client/lib/contrib/backo2.ts | 4 +- packages/socket.io-client/lib/manager.ts | 6 +-- packages/socket.io-client/test/connection.ts | 39 +++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/packages/socket.io-client/lib/contrib/backo2.ts b/packages/socket.io-client/lib/contrib/backo2.ts index 2307eff461..544ce6611e 100644 --- a/packages/socket.io-client/lib/contrib/backo2.ts +++ b/packages/socket.io-client/lib/contrib/backo2.ts @@ -12,8 +12,8 @@ export function Backoff(opts) { opts = opts || {}; - this.ms = opts.min || 100; - this.max = opts.max || 10000; + this.ms = opts.min ?? 100; + this.max = opts.max ?? 10000; this.factor = opts.factor || 2; this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0; this.attempts = 0; diff --git a/packages/socket.io-client/lib/manager.ts b/packages/socket.io-client/lib/manager.ts index b9597bf233..d366fcbeca 100644 --- a/packages/socket.io-client/lib/manager.ts +++ b/packages/socket.io-client/lib/manager.ts @@ -169,9 +169,9 @@ export class Manager< this.opts = opts; installTimerFunctions(this, opts); this.reconnection(opts.reconnection !== false); - this.reconnectionAttempts(opts.reconnectionAttempts || Infinity); - this.reconnectionDelay(opts.reconnectionDelay || 1000); - this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000); + this.reconnectionAttempts(opts.reconnectionAttempts ?? Infinity); + this.reconnectionDelay(opts.reconnectionDelay ?? 1000); + this.reconnectionDelayMax(opts.reconnectionDelayMax ?? 5000); this.randomizationFactor(opts.randomizationFactor ?? 0.5); this.backoff = new Backoff({ min: this.reconnectionDelay(), diff --git a/packages/socket.io-client/test/connection.ts b/packages/socket.io-client/test/connection.ts index 1cf3bf01f4..1944dc36dd 100644 --- a/packages/socket.io-client/test/connection.ts +++ b/packages/socket.io-client/test/connection.ts @@ -403,6 +403,45 @@ describe("connection", () => { }); }); + it("should accept 0 as the value of the reconnection options", () => { + const manager = new Manager(BASE_URL, { + autoConnect: false, + reconnectionAttempts: 0, + reconnectionDelay: 0, + reconnectionDelayMax: 0, + }); + + expect(manager.reconnectionAttempts()).to.be(0); + expect(manager.reconnectionDelay()).to.be(0); + expect(manager.reconnectionDelayMax()).to.be(0); + // @ts-ignore property 'backoff' is private + expect(manager.backoff.duration()).to.be(0); + }); + + it("should not try to reconnect when requested zero attempts", () => { + return wrap((done) => { + const manager = new Manager(BASE_URL, { + reconnection: true, + timeout: 0, + reconnectionAttempts: 0, + reconnectionDelay: 10, + }); + let socket; + + manager.on("reconnect_attempt", () => { + done(new Error("should not happen")); + }); + + manager.on("reconnect_failed", () => { + socket.close(); + manager._close(); + done(); + }); + + socket = manager.socket("/timeout"); + }); + }); + it("should fire reconnect_* events on manager", () => { return wrap((done) => { const manager = new Manager(BASE_URL, {