From 522ffbe7a89dec6ad0abf272992a487b6d47a1ec Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 18 Nov 2021 11:59:01 +0100 Subject: [PATCH] fix: prevent double ack with timeout The ack was not properly removed upon timeout, and could be called twice. Related: https://github.com/socketio/socket.io-client/commit/ccf7998cc5049d02022567aedfb263de875a06a5 --- lib/socket.ts | 1 + test/socket.ts | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/socket.ts b/lib/socket.ts index a88a5433..3e5de480 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -205,6 +205,7 @@ export class Socket< // @ts-ignore const timer = this.io.setTimeoutFn(() => { + delete this.acks[id]; for (let i = 0; i < this.sendBuffer.length; i++) { if (this.sendBuffer[i].id === id) { debug("removing packet with ack id %d from the buffer", id); diff --git a/test/socket.ts b/test/socket.ts index 0c1f0031..5092b32f 100644 --- a/test/socket.ts +++ b/test/socket.ts @@ -382,7 +382,7 @@ describe("socket", function () { }); }); - it("should timeout after the given delay when server does not acknowledge the event", (done) => { + it("should timeout when the server does not acknowledge the event", (done) => { const socket = io("/"); socket.timeout(50).emit("unknown", (err) => { @@ -391,7 +391,23 @@ describe("socket", function () { }); }); - it("should not timeout after the given delay when server does acknowledge", (done) => { + it("should timeout when the server does not acknowledge the event in time", (done) => { + const socket = io("/"); + + let count = 0; + + socket.timeout(0).emit("echo", 42, (err) => { + expect(err).to.be.an(Error); + count++; + }); + + setTimeout(() => { + expect(count).to.eql(1); + success(done, socket); + }, 200); + }); + + it("should not timeout when the server does acknowledge the event", (done) => { const socket = io("/"); socket.timeout(50).emit("echo", 42, (err, value) => {