fix: prevent double ack when emitting with a timeout

The ack was not properly removed upon timeout, and could be called
twice.

Related: f0ed42f18c
This commit is contained in:
Damien Arrachequesne
2021-11-18 12:39:06 +01:00
parent f0ed42f18c
commit b839a3b400
2 changed files with 24 additions and 0 deletions

View File

@@ -234,6 +234,7 @@ export class Socket<
const timer = setTimeout(() => {
debug("event with ack id %d has timed out after %d ms", id, timeout);
this.acks.delete(id);
ack.call(this, new Error("operation has timed out"));
}, timeout);

View File

@@ -15,6 +15,29 @@ describe("timeout", () => {
});
});
it("should timeout if the client does not acknowledge the event in time", (done) => {
const io = new Server(0);
const client = createClient(io, "/");
client.on("echo", (arg, cb) => {
cb(arg);
});
let count = 0;
io.on("connection", (socket) => {
socket.timeout(0).emit("echo", 42, (err) => {
expect(err).to.be.an(Error);
count++;
});
});
setTimeout(() => {
expect(count).to.eql(1);
success(done, io, client);
}, 200);
});
it("should not timeout if the client does acknowledge the event", (done) => {
const io = new Server(0);
const client = createClient(io, "/");