From 2ca998011490d9fc0265ceecf061c4b8a2ea01ab Mon Sep 17 00:00:00 2001 From: Mark Russell Date: Fri, 13 Nov 2026 00:45:52 -0500 Subject: [PATCH] fix(eio-client): clamp the delay of the heartbeat timer The heartbeat timer was created with a delay of pingInterval + pingTimeout. When the sum was above 2^31 - 1 ms (about 24.8 days), setTimeout() fired immediately, so the client closed the connection with the "ping timeout" reason right after the handshake. The server accepts these values, since each of its timers uses only one of them. Co-Authored-By: Claude Opus 5.5 --- packages/engine.io-client/lib/socket.ts | 8 +++++++- packages/engine.io-client/test/node.js | 27 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/engine.io-client/lib/socket.ts b/packages/engine.io-client/lib/socket.ts index 2bd3603c2..ee028caea 100644 --- a/packages/engine.io-client/lib/socket.ts +++ b/packages/engine.io-client/lib/socket.ts @@ -22,6 +22,9 @@ const withEventListeners = const OFFLINE_EVENT_LISTENERS = []; +// the maximum delay of setTimeout() (about 24.8 days), a larger value makes the timer fire immediately +const MAX_TIMER_DELAY = 2147483647; + if (withEventListeners) { // within a ServiceWorker, any event handler for the 'offline' event must be added on the initial evaluation of the // script, so we create one single event listener here which will forward the event to the socket instances @@ -819,7 +822,10 @@ export class Socket extends Emitter< */ private _resetPingTimeout() { this.clearTimeoutFn(this._pingTimeoutTimer); - const delay = this._pingInterval + this._pingTimeout; + const delay = Math.min( + this._pingInterval + this._pingTimeout, + MAX_TIMER_DELAY, + ); this._pingTimeoutTime = Date.now() + delay; this._pingTimeoutTimer = this.setTimeoutFn(() => { this._onClose("ping timeout"); diff --git a/packages/engine.io-client/test/node.js b/packages/engine.io-client/test/node.js index 1f756a13b..f84409206 100644 --- a/packages/engine.io-client/test/node.js +++ b/packages/engine.io-client/test/node.js @@ -1,5 +1,7 @@ const path = require("node:path"); const { execFile } = require("node:child_process"); +const { createServer } = require("node:http"); +const { attach } = require("engine.io"); const { Socket } = require("../"); const { repeat } = require("./util"); const expect = require("expect.js"); @@ -109,6 +111,31 @@ describe("node.js", () => { }); }); + it("should not close when pingInterval + pingTimeout is above the maximum timer delay", (done) => { + const httpServer = createServer(); + const engine = attach(httpServer, { + pingInterval: 25000, + pingTimeout: 2147483647, // the maximum delay of setTimeout() + }); + + httpServer.listen(0, () => { + const socket = new Socket( + `http://localhost:${httpServer.address().port}`, + { transports: ["websocket"] }, + ); + + socket.on("open", () => { + setTimeout(() => { + expect(socket.readyState).to.eql("open"); + socket.close(); + engine.close(); + httpServer.close(); + done(); + }, 100); + }); + }); + }); + it("should not modify the extraHeaders option with withCredentials: true", (done) => { const extraHeaders = { foo: "bar" }; const socket = new Socket("http://localhost:3000", {