Skip to content

Commit a79f28c

Browse files
authored
clear timeout on rejected command (#36)
1 parent f75fdc0 commit a79f28c

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/Command.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ export default class Command implements Respondable {
339339
} else {
340340
this.reject = reject;
341341
}
342+
}).finally(() => {
343+
const existingTimer = this._commandTimeoutTimer;
344+
if (existingTimer) {
345+
clearTimeout(existingTimer);
346+
delete this._commandTimeoutTimer;
347+
}
342348
});
343349

344350
this.promise = asCallback(promise, this.callback);
@@ -370,12 +376,6 @@ export default class Command implements Respondable {
370376
private _convertValue(resolve: Function): (result: any) => void {
371377
return (value) => {
372378
try {
373-
const existingTimer = this._commandTimeoutTimer;
374-
if (existingTimer) {
375-
clearTimeout(existingTimer);
376-
delete this._commandTimeoutTimer;
377-
}
378-
379379
resolve(this.transformReply(value));
380380
this.isResolved = true;
381381
} catch (err) {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/functional/commandTimeout.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,27 @@ describe("commandTimeout", () => {
3434
redis.disconnect();
3535
await server.disconnectPromise();
3636
});
37+
38+
it("does not leak timers on rejected commands", async () => {
39+
const server = new MockServer(30001, (argv, socket, flags) => {
40+
if (argv[0] === "evalsha") {
41+
return new Error("test error");
42+
}
43+
});
44+
45+
const redis = new Redis({ port: 30001, commandTimeout: 1000000000 });
46+
const clock = sinon.useFakeTimers();
47+
let error: any;
48+
try {
49+
await redis.evalsha("asd", 0);
50+
} catch (err) {
51+
error = err;
52+
}
53+
54+
expect(error.message).to.eql("test error");
55+
expect(clock.countTimers()).to.eql(0);
56+
clock.restore();
57+
redis.disconnect();
58+
server.disconnect();
59+
});
3760
});

0 commit comments

Comments
 (0)