Skip to content

Commit b839a3b

Browse files
fix: prevent double ack when emitting with a timeout
The ack was not properly removed upon timeout, and could be called twice. Related: f0ed42f
1 parent f0ed42f commit b839a3b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/socket.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export class Socket<
234234

235235
const timer = setTimeout(() => {
236236
debug("event with ack id %d has timed out after %d ms", id, timeout);
237+
this.acks.delete(id);
237238
ack.call(this, new Error("operation has timed out"));
238239
}, timeout);
239240

test/socket-timeout.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ describe("timeout", () => {
1515
});
1616
});
1717

18+
it("should timeout if the client does not acknowledge the event in time", (done) => {
19+
const io = new Server(0);
20+
const client = createClient(io, "/");
21+
22+
client.on("echo", (arg, cb) => {
23+
cb(arg);
24+
});
25+
26+
let count = 0;
27+
28+
io.on("connection", (socket) => {
29+
socket.timeout(0).emit("echo", 42, (err) => {
30+
expect(err).to.be.an(Error);
31+
count++;
32+
});
33+
});
34+
35+
setTimeout(() => {
36+
expect(count).to.eql(1);
37+
success(done, io, client);
38+
}, 200);
39+
});
40+
1841
it("should not timeout if the client does acknowledge the event", (done) => {
1942
const io = new Server(0);
2043
const client = createClient(io, "/");

0 commit comments

Comments
 (0)