Skip to content

Commit

Permalink
fix: prevent double ack with timeout
Browse files Browse the repository at this point in the history
The ack was not properly removed upon timeout, and could be called
twice.

Related: ccf7998
  • Loading branch information
darrachequesne committed Nov 18, 2021
1 parent 99c2cb8 commit 522ffbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/socket.ts
Expand Up @@ -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);
Expand Down
20 changes: 18 additions & 2 deletions test/socket.ts
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down

0 comments on commit 522ffbe

Please sign in to comment.